【发布时间】:2020-12-16 15:58:06
【问题描述】:
我需要过滤掉所有不以 _y 或 _x 结尾的列。
merged_hdi_aidg=df_HDI.merge(df_FDAC_CA, on='iso3_codes')
merged_hdi_aidg.columns
索引(['Country_x', '1990', '1991', '1992', '1993', '1994', '1995', '1996', '1997'、'1998'、'1999'、'2000'、'2001_x'、'2002_x'、'2003_x'、'2004_x'、 '2005_x'、'2006_x'、'2007_x'、'2008_x'、'2009_x'、'2010_x'、'2011_x'、 '2012_x'、'2013_x'、'2014_x'、'2015_x'、'2016_x'、'2017_x'、'2018_x'、 'iso3_codes'、'Country_y'、'2001_y'、'2002_y'、'2003_y'、'2004_y'、 '2005_y'、'2006_y'、'2007_y'、'2008_y'、'2009_y'、'2010_y'、'2011_y'、 '2012_y'、'2013_y'、'2014_y'、'2015_y'、'2016_y'、'2017_y'、'2018_y'、 '2019', '2020'], dtype='object')
drop_columns=[]
[drop_columns.append(column) for column in merged_hdi_aidg.columns if ('_y' not in column or '_x' not in column)];
drop_columns
['Country_x', '1990', '1991', '1992', '1993', '1994', '1995', '1996', '1997', '1998', '1999', '2000', '2001_x', '2002_x', '2003_x', '2004_x', '2005_x', '2006_x', '2007_x', '2008_x', '2009_x', '2010_x', '2011_x', '2012_x', '2013_x', '2014_x', '2015_x', '2016_x', '2017_x', '2018_x', 'iso3_codes', 'Country_y', '2001_y', '2002_y', '2003_y', '2004_y', '2005_y', '2006_y', '2007_y', '2008_y', '2009_y', '2010_y', '2011_y', '2012_y', '2013_y', '2014_y', '2015_y', '2016_y', '2017_y', '2018_y', '2019', '2020']
【问题讨论】:
-
嗨 Clayton,我们的解决方案对您有帮助吗?
-
是的,他们都做到了。谢谢!
标签: python-3.x list if-statement