简体   繁体   中英

For in loop creating new Pandas DataFrames

I have 3 pd.DataFrame and I want to apply the same operation (with a loop) creating 3 new DataFrames.

d = {}
dfs=[d10,d11,acc]
for i in dfs:
    a = (i.groupby('Variable').cumcount()).reset_index()
    d[i] = pd.DataFrame(a)

I've tried to save it in a dictionary buT the error I've obtained is:

TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed

Store them in a list. Not in a dictionary.

dfs=[d10,d11,acc]
for i in dfs:
    a = (i.groupby('Variable').cumcount()).reset_index()
    d.append( pd.DataFrame(a) )

Then, for calling each DataFrame:

d[0]-->d10

d[1]-->d11

d[2]-->acc

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