简体   繁体   中英

loop over names of several pandas DataFrames

I have a couple of DataFrames from different files, which are named for example df001, df002 and so on. Now I want to loop over those DataFrames to execute similar tasks. But I can't figure out how to address them.

This failed (AttributeError: 'str' object has no attribute 'iloc'):

names = ['df001', 'df002']

for name in names:
    name.iloc[1,1]

Can you try this?

names = [df001, df002]

for name in names:
    name.iloc[1,1]

If you use the string name for purposes other than looping, you can always store the dataframes in a dictionary:

d = {'df001': df001, 'df002': df002}

for name in d:
    d[name].iloc[1, 1]

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