Python利用pandas计算多个CSV文件数据值

功能:扫描当前目录下所有CSV文件并对其中文件进行统计,输出统计值到CSV文件
pip install pandas

import pandas as pd
import glob,os,sys
input_path='./'
output_fiel='pandas_union_concat.csv'
all_files=glob.glob(os.path.join(input_path,'sales_*'))
all_data_frames=[]
for file in all_files:
    data_frame=pd.read_csv(file,index_col=None)
    total_sales=pd.DataFrame([float(str(value).strip('$').replace(',','')) for value in data_frame.loc[:,'Sale Amount']]).sum()
    average_sales=pd.DataFrame([float(str(value).strip('$').replace(',','')) for value in data_frame.loc[:,'Sale Amount']]).mean()
    data={
        'filename':os.path.basename(file),
        'total_sales':total_sales,
        'average_sales':average_sales
    }
    all_data_frames.append(pd.DataFrame(data,columns=['filename','total_sales','average_sales']))
data_frame_concat=pd.concat(all_data_frames,axis=0,ignore_index=True)
data_frame_concat.to_csv(output_fiel,index=False)

你可能感兴趣的:(python)