如何使用循环从多个熊猫数据框中删除一列?

时间:2019-05-23 15:09:37

标签: python pandas

我在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]

我想使用循环从所有这些数据框中删除一个名为“ Plot”的列。 我该怎么做?

我尝试了以下操作,但这不起作用

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

2 个答案:

答案 0 :(得分:1)

这应该有效:

for df in CGdfs:
    df.drop(columns = ['Plot'], inplace= True)

答案 1 :(得分:1)

我认为问题在于它没有将拖放应用于原始DataFrame对象。试试:

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

检查CGdfs的元素时,应删除“图”列。