简体   繁体   English

遍历并创建新的数据框

[英]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.

相关问题 如何遍历数据框,创建新列并在python中为其添加值 - How to loop through a dataframe, create a new column and append values to it in python 遍历PySpark DataFrame和创建新列的更有效方法 - More efficient way to loop through PySpark DataFrame and create new columns 遍历 pandas dataframe 的列并为循环中的每个选定列创建一个新的 dataframe - Iterate through columns of pandas dataframe and create a new dataframe for each selected column in a loop 解析数据框列以创建新的数据框 - Parse through dataframe column to create new dataframe 遍历一个数据框中的单个列与另一个数据框中的列进行比较使用熊猫在第一个数据框中创建新列 - loop through a single column in one dataframe compare to a column in another dataframe create new column in first dataframe using pandas 循环遍历 dataframe 的行,创建一个新列并根据条件将结果存储在另一列中 - Loop through rows of a dataframe, create a new column and store the result based on condition in another column 如何遍历数据框并创建目录python - How to loop through a dataframe and create directories python 循环遍历参数字典并创建数据框值 - Loop through parameter dictionary and create dataframe values 循环通过 Pandas dataframe 并根据条件复制到新的 dataframe - Loop through Pandas dataframe and copying to a new dataframe based on a condition 循环将创建新的Pandas.DataFrame列 - Loop that will create new Pandas.DataFrame column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM