遍历并创建新的数据框

时间:2019-03-09 13:15:22

标签: python pandas loops numpy dataframe

我有一个共享文件夹,用于存储.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

现在我想要一个如下所示的新数据框。

也文件名不应该是完整路径。

enter image description here

1 个答案:

答案 0 :(得分:0)

尝试以下方法,它可以起作用:

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