Python利用pandas计算多个CSV文件数据值的实例
作者:卒迹 时间:2022-11-21 23:00:26
功能:扫描当前目录下所有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)
来源:https://blog.csdn.net/u010129985/article/details/79177359
标签:pandas,CSV,Python,数据值
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python中subprocess模块用法实例详解
2021-05-30 20:43:12
基于bootstrop常用类总结(推荐)
2024-04-17 09:52:42
![](https://img.aspxhome.com/file/2023/2/136472_0s.png)
将Python文件打包成.EXE可执行文件的方法
2023-05-26 20:25:50
Python利用Scrapy框架爬取豆瓣电影示例
2022-04-30 15:16:17
![](https://img.aspxhome.com/file/2023/3/86253_0s.png)
TensorFlow的权值更新方法
2022-12-24 21:41:08
css命名及书写规范大全
2008-05-24 08:52:00
![](https://img.aspxhome.com/file/UploadPic/up/2008052409352233.gif)
分享4个方便且好用的Python自动化脚本
2023-05-31 02:20:32
Python Json模块中dumps、loads、dump、load函数介绍
2021-03-20 11:21:20
![](https://img.aspxhome.com/file/2023/9/100989_0s.jpg)
关于Python包导入报错的问题总结
2021-02-05 18:13:46
![](https://img.aspxhome.com/file/2023/7/75257_0s.png)
python3 Scrapy爬虫框架ip代理配置的方法
2021-03-01 00:39:11
![](https://img.aspxhome.com/file/2023/7/131137_0s.png)
pandas如何删除没有列名的列浅析
2021-05-17 11:08:47
![](https://img.aspxhome.com/file/2023/4/118064_0s.png)
SQL语句练习实例之七 剔除不需要的记录行
2011-11-03 16:50:51
Python使用django获取用户IP地址的方法
2021-07-22 04:01:05
解决Python spyder显示不全df列和行的问题
2021-06-23 00:15:47
利用Python编写个有趣的记仇本
2022-08-25 19:20:20
![](https://img.aspxhome.com/file/2023/7/115117_0s.png)
Python实现猜数字小游戏
2022-10-09 18:38:12
![](https://img.aspxhome.com/file/2023/1/99261_0s.jpg)
python中lambda()的用法
2022-07-19 05:15:45
python中logging模块的一些简单用法的使用
2023-08-12 09:28:17
![](https://img.aspxhome.com/file/2023/6/94186_0s.png)
python元组和字典的内建函数实例详解
2021-09-13 09:34:56
windows下Mysql多实例部署的操作方法
2024-01-19 16:33:11