簡體   English   中英

遍歷並創建新的數據框

[英]Loop through and create and new 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

現在我想要一個如下的新數據框。

另外文件名也不應該是完整路徑。

在此輸入圖像描述

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

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM