首页 > 解决方案 > 循环并创建新的数据框

问题描述

我有一个存储 .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

现在我想要一个新的数据框如下..

文件名也不应该是整个路径..

在此处输入图像描述

标签: pythonpandasloopsnumpydataframe

解决方案


试试下面,它的工作原理:

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})

推荐阅读