简体   繁体   中英

Python - How to write a loop to add a column for every df in a list based on another list's element python

I have a list of dataframes and I want to add a new column named 'new_index' for each df in that list. The 'new_index' is based on another list.

lst_dfs = [(pd.DataFrame({'country':['a','b','c','d'],
  'gdp':[1,2,3,4],
  'iso':['x','y','z','w']})),
  (pd.DataFrame({'country':['aa','bb','cc','dd'],
  'gdp':[11,22,33,44],
  'iso':['xx','yy','zz','ww']})),
  (pd.DataFrame({'country':['aaa','bbb','ccc','ddd'],
  'gdp':[111,222,333,444],
  'iso':['xxx','yyy','zzz','www']}))

lst_index = ['index1','index2','index3']

print(lst_dfs[0])
>>>
country gdp iso
0   a   1   x
1   b   2   y
2   c   3   z
3   d   4   w

Expected outputs:

country gdp iso new_index
0   a   1   x   index1
1   b   2   y   index1
2   c   3   z   index1
3   d   4   w   index1


country gdp iso new_index
0   aa  11  xx  index2
1   bb  22  yy  index2
2   cc  33  zz  index2
3   dd  44  ww  index2

country gdp iso new_index
0   aaa 111 xxx index3
1   bbb 222 yyy index3
2   ccc 333 zzz index3
3   ddd 444 www index3

Can anyone help me with the problem? Thanks so much.

You can use zip :

for df, idx in zip(lst_dfs, lst_index): df['new_index'] = idx

print(lst_dfs[1])

Output:

  country  gdp iso new_index
0      aa   11  xx    index2
1      bb   22  yy    index2
2      cc   33  zz    index2
3      dd   44  ww    index2

Here is what you can do:

import pandas as pd

lst_dfs = [(pd.DataFrame({'country':['a','b','c','d'],
                          'gdp':[1,2,3,4],
                          'iso':['x','y','z','w']})),
           
           (pd.DataFrame({'country':['aa','bb','cc','dd'],
                          'gdp':[11,22,33,44],
                          'iso':['xx','yy','zz','ww']})),
           
           (pd.DataFrame({'country':['aaa','bbb','ccc','ddd'],
                          'gdp':[111,222,333,444],
                          'iso':['xxx','yyy','zzz','www']}))]

lst_index = ['index1','index2','index3']

lst_dfs = [{k:list(v) for k,v in d.to_dict().items()} for d in lst_dfs]

for i,d in enumerate(lst_dfs):
    
    d.update({'new_index':[lst_index[i] for _ in range(4)]})
    
    print(pd.DataFrame(d))

Output:

   country  gdp  iso new_index
0        0    0    0    index1
1        1    1    1    index1
2        2    2    2    index1
3        3    3    3    index1

   country  gdp  iso new_index
0        0    0    0    index2
1        1    1    1    index2
2        2    2    2    index2
3        3    3    3    index2

   country  gdp  iso new_index
0        0    0    0    index3
1        1    1    1    index3
2        2    2    2    index3
3        3    3    3    index3

Let us try concat with keys

newl=[y.reset_index(0) for _ , y in pd.concat(lst_dfs,keys=lst_index).groupby(level=0)]

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