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)
相关推荐
wangquannuaa 2020-10-15
jzlixiao 2020-05-15
QianYanDai 2020-05-07
三石 2020-10-30
roamer 2020-10-29
三石 2020-10-29
wangquannuaa 2020-09-29
jzlixiao 2020-09-15
wangquannuaa 2020-08-30
三石 2020-08-23
逍遥友 2020-08-21
jzlixiao 2020-08-18
wangquannuaa 2020-08-17
QianYanDai 2020-08-16
cjsyrwt 2020-08-14
jzlixiao 2020-07-29
xirongxudlut 2020-07-20
mmmjyjy 2020-07-16
QianYanDai 2020-07-05