[英]Loop through and create and new dataframe
I have a Shared folder where .CSV files are be stored... i will take all the .CSV file to do my manipulation 我有一个共享文件夹,其中存储.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
Now i want a new Data Frame as below.. 现在我想要一个如下的新数据框。
also file name should not be entire path.. 另外文件名也不应该是完整路径。
Try Below, it works: 试试下面,它的工作原理:
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})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.