简体   繁体   English

Pandas 使用 for 循环创建多个数据框

[英]Pandas Creating multiple data frames using for loop

I am looking for efficient Python codes to:我正在寻找高效的 Python 代码来:

1. Create multiple data frames 2. Loop over the multiple data frames 1. 创建多个数据框 2. 循环多个数据框

For instance, in my code:例如,在我的代码中:

os.chdir(EU_path)
csv_files = glob.glob(EU_path + '\*.csv')
list_EU_data = []
for filename in csv_files:
    data = pd.read_csv(filename)
    list_EU_data.append(data)

list_EU_data is a list of 10 csv files of product sales from 10 European countries. list_EU_data是来自 10 个欧洲国家的产品销售的 10 个 csv 文件的列表。 For instance, list_EU_data[0] returns a data frame with columns related to sales information.例如, list_EU_data[0]返回一个数据框,其中包含与销售信息相关的列。

Here, I want to create multiple data frames while pre-processing data frames, for instance,在这里,我想在预处理数据帧的同时创建多个数据帧,例如,

First select columns首先选择列

EU[0] = list_EU_data[0].iloc[:, [0, 1]]
EU[1] = list_EU_data[1].iloc[:, [0, 1]]
...continues...
EU[9] = list_EU_data[9].iloc[:, [0, 1]]

Next, from each of the data frames, I want to replace 0 values by 1 and iterate all the data frames:接下来,从每个数据帧,我想用 1 替换 0 值并迭代所有数据帧:

EU[0].iloc[:, 1] = EU[0].iloc[:, 1].replace(0, 1)
EU[1].iloc[:, 1] = EU[1].iloc[:, 1].replace(0, 1)
...continues...
EU[9].iloc[:, 1] = EU[9].iloc[:, 1].replace(0, 1)

Using for loop, what's the most efficient ways to write the above code?使用 for 循环,编写上述代码的最有效方法是什么?

If need loop solution working with list of DataFrame s:如果需要使用DataFrame列表的循环解决方案:

for i, df1 in enumerate(EU):
    df1 = df1.iloc[:, [0, 1]]
    df1.iloc[:, 1] = df1.iloc[:, 1].replace(0, 1)
    EU[i] = df1

Also is possible change your code:也可以更改您的代码:

os.chdir(EU_path)
csv_files = glob.glob(EU_path + '\*.csv')
list_EU_data = []
for filename in csv_files:
    data = pd.read_csv(filename)
    data = data.iloc[:, [0, 1]]
    data.iloc[:, 1] = data.iloc[:, 1].replace(0, 1)
    list_EU_data.append(data)

请参考我的评论: https : //stackoverflow.com/a/64798278/9967586用于创建多个数据框并循环遍历这些 dfs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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