python - 循环并创建新的数据框
问题描述
我有一个存储 .CSV 文件的共享文件夹...我将使用所有 .CSV 文件进行操作
import glob
x = glob.glob(r'C:\Users\Desktop\files\*.csv')
# x has path of all the file, say i have 3 file in folder
i=0
while i < len(x):
df=pd.read_csv(x[i],header=1)
#x[i] is full file path,so now we assumed we have 3 files
..
# Some data manipulation
..
print(avg)
# with 3 file, 3 different AVG value calculated
print(sum)
# with 3 file, 3 different SUM value calculated
i += 1
现在我想要一个新的数据框如下..
文件名也不应该是整个路径..
解决方案
试试下面,它的工作原理:
import glob
x = glob.glob(r'C:\Users\Desktop\files\*.csv')
i=0
avglist = []
sumlist = []
while i < len(x):
df=pd.read_csv(x[i],header=1)
#x[i] is full file path
..
# Some data manipulation
..
#print(avg)
avglist.append(avg)
#print(sum)
sumlist.append(sum)
i += 1
df = pd.DataFrame({"File Name": x, "Average": avglist, "Sum": sumlist})
推荐阅读
- javascript - jQuery - 自动提交表单
- java - 实现特定接口的通用类
- windows - 加入 2 个 CSV 的最简单方法(如 SQL LEFT JOIN)
- c# - System.BadImageFormatException:试图加载格式不正确的程序。(来自 HRESULT 的异常:0x8007000B)
- angular - 是否可以在 Angular 中创建通用 toast 组件?
- javascript - 如何在 JavaScript 中使用正则表达式匹配下面的字符串?[不要专注于重复,审稿人太评判]
- python - PyCharm 没有 AWS Glue Python 的自动完成功能
- firepad - 如何在 mongo 数据库中使用 firepad js
- firebase - 如何对聊天应用程序的数据实时数据库进行分片?
- css - 为样式创建指令是否会被视为反模式