2

I have multiple dataframes in a list 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 want to drop a column named 'Plot' from all these dataframes using a loop. How do I do this?

I have tried the below, this does not work

for df in CGdfs:
   df = df.drop('Plot', axis =1)
cs95
  • 379,657
  • 97
  • 704
  • 746
user3021495
  • 97
  • 10

2 Answers2

1

This should work:

for df in CGdfs:
    df.drop(columns = ['Plot'], inplace= True)
cs95
  • 379,657
  • 97
  • 704
  • 746
1

I think the problem is that it's not applying the drop to the original DataFrame object. Try:

for df in CGdfs:
    df.drop('Plot', axis=1, inplace=True)

When you inspect the elements of CGdfs the "Plot" column should be removed.

iamchoosinganame
  • 1,090
  • 6
  • 15