Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 440
Bangbangbang Avatar asked Dec 03 '25 04:12

Bangbangbang


1 Answers

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
like image 87
Quang Hoang Avatar answered Dec 04 '25 20:12

Quang Hoang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!