【问题标题】:How to use OR in a list comprehension IF condition如何在列表理解 IF 条件中使用 OR
【发布时间】: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


【解决方案1】:

你也可以反过来,选择那些包含_x和_y的:

示例数据

merged_hdi_aidg = pd.DataFrame({'x1_y':[1,2], 'x1_x':[1,3], 'x1_z':[0,0]})

使用str.contains:

merged_hdi_aidg.loc[:, merged_hdi_aidg.columns.str.contains('|'.join(['_y', '_x']))]

如果您想确保只捕获列名末尾的 _x 和 _y,请使用 endswith

merged_hdi_aidg.loc[:, merged_hdi_aidg.columns.str.endswith(('_x','_y'))]  

【讨论】:

  • 这也解决了我的问题!这也让我问为什么你需要 ,join() 并且不能只使用一个列表?我尝试了这个列表,但出现了一个错误......不过,你展示它的方式很好,谢谢!
  • 连接部分'|'.join(['_y', '_x']) 使其成为'_y|_x',这是您在str.contains 中测试多个字符串所需的格式。因此,连接部分允许您将列表输入为'|'.join(yourlist)
【解决方案2】:

这是一种可能的方法。我稍微修改了你的代码。 步骤

  • 制作一组所有列
  • 制作一组您希望丢弃的所有列 ..(具有 模式)
  • 得到 1 和 2 的差值。
  • 完成
merged_hdi_aidg=df_HDI.merge(df_FDAC_CA, on='iso3_codes')
AllColumns = set(merged_hdi_aidg.columns)
drop_columns=[]
[drop_columns.append(column) for column in merged_hdi_aidg.columns if ('_y' in column or '_x' in column)];
drop_columns = set(drop_columns)
ColumnsThatYouWant = AllColumns - drop_columns
print(ColumnsThatYouWant)

它给出以下输出

{'1998', '1992', '1999', '2020', '1993', '1991', '1990', 'iso3_codes', '2000', '1995', '1997', '2019', '1996', '1994'}

您可能想将其转换回列表或其他内容。 顺便说一句,您没有在列名的末尾检查“_x”或“_y”。您仅检查它们是否存在于列名中的任何位置。也许你也想解决这个问题。

【讨论】:

  • 谢谢!这段代码工作正常,但给了我想要删除的列,而不是我想要保留的列。我只需要对其进行一些重组,它就可以正常工作!
【解决方案3】:

解决方案可能是这样的:

x = ['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']


columns_to_take = [i for i in x if i.endswith('_x') or i.endswith('_y')]

print("columns_to_take = ", columns_to_take)

columns_to_drop = [i for i in x if (not i.endswith('_x')) and (not i.endswith('_y'))]

print("columns_to_drop = " , columns_to_drop)

【讨论】:

    猜你喜欢
    • 2010-12-05
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2012-05-03
    相关资源
    最近更新 更多