0

I have 17 data frames to which I wish to add a new column using for loop based on certain conditions. Any suggestions on how this can be done?

I have a list called custom_region = [] and 17 dataframes again stored in a list called CGdfs.

CGdfs = [CGdf_2002, CGdf_2003, CGdf_2004, CGdf_2005, CGdf_2006, CGdf_2007, CGdf_2008, CGdf_2009, CGdf_2010, CGdf_2011, CGdf_2012, CGdf_2013, CGdf_2014, CGdf_2015, CGdf_2016, CGdf_2017, CGdf_2018]

I am appending new values to custom_region based on certain conditions in a loop. This list I have to finally append to each of these dataframes to create a column for each of them.

So, custom_region[0] to CGdf_2002
custom_region[1] to CGdf_2003 and so on..

GileBrt
  • 1,830
  • 3
  • 20
  • 28
user3021495
  • 97
  • 10

1 Answers1

0

I didn't understand your question really well , but I suppose that your custom_region is already filled with columns you want to append to CGdf dataframes.

If so you have to loop through CGdf and for each dataframe you will concat a column from custom_region

how about this :

i = 0
j = 0 

for i in range(len(CGdfs)) : 
  CGdfs[i] = pd.concat([CGdfs[i],custom_region[j]],axis=1)
  j = j + 1 



nassim
  • 1,547
  • 1
  • 14
  • 26
  • Hi, thanks for this. I don't have any columns in custom_region. It is an empty list I will be adding my column values to in the loop. – user3021495 Apr 08 '19 at 12:20