简体   繁体   English

如何循环更新许多熊猫数据框

[英]How to update many pandas dataframes in a loop

I'm working with many dataframes and I need to update all of them in the same way. 我正在处理许多数据框,因此需要以相同的方式更新所有这些数据框。 I think it would be easier to do it in a loop rather than write a unique line for each change. 我认为循环执行此操作要比为每个更改编写唯一的行要容易。 But I can't manage to update the dataframes. 但是我无法更新数据框。

Here is what I've tried (but doesn't work) 这是我尝试过的(但不起作用)

pd1=pd.read_csv('data1.csv')
pd2=pd.read_csv('data2.csv')

samples = [pd1,pd2]

for i, df in enumerate(samples):
    samples[i] = samples[i].truncate(after=99, axis="rows")

Any ideas how to update the changes or iterate through dataframes in a proper way? 有什么想法如何以适当的方式更新更改或遍历数据框吗?

This will overwrite your files though, so if you want to preserve the orginial, you'll need to save as a different file name, like: 但是,这将覆盖文件,因此,如果要保留原始文件,则需要另存为其他文件名,例如:

pd.read_csv(file, nrows=100).to_csv('trunc_'+ file, index=False)

But you can iterate through: 但是您可以遍历:

import pandas as pd

filenames = ['data1.csv','data2.csv']

for file in filenames:
    pd.read_csv(file, nrows=100).to_csv(file, index=False)

Assuming 2 dfs as : 假设2 dfs为:

print(df1)
   col1  col2 col3 col4
0     1     2    A    S
1     3     4    A    P
2     5     6    B    R
3     7     8    B    B

print(df2)
   col5  col6 col3
0     9    10    A
1    11    12    R

you can update and store in a dictionary as: 您可以按以下方式更新和存储在字典中:

samples  = [df1,df2]
frames={}
for e,i in enumerate(samples):
    frames.update([('df'+str(e+1), i.truncate(after=2, axis="rows"))])

print(frames)

{'df1':    col1  col2 col3 col4
0     1     2    A    S
1     3     4    A    P
2     5     6    B    R, 'df2':    col5  col6 col3
0     9    10    A
1    11    12    R}

Now just call the key to get your dataframe: 现在只需调用密钥即可获取数据框:

print(frames['df1'])

   col1  col2 col3 col4
0     1     2    A    S
1     3     4    A    P
2     5     6    B    R

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

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