简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM