![](/img/trans.png)
[英]How to filter the pandas dataframe when one cell value of all columns equals to a specific string
[英]Obtain the pandas dataframe column names in a list when one cell value equals to a specific string
我有一个大约 20-30 列的 pandas dataframe。 当一个单元格值等于字符串“失败”时,我想获取列表中的列名。
dataframe 看起来像这样:
Column_1 Column_2 ... Column_30
0 Pass Pass Fail
1 Pass Pass Pass
2 Fail Pass Pass
3 Pass Pass Pass
4 Pass Pass Pass
..
预期结果看起来像这样[Column_1, Column 30]
我认为这应该可以解决问题
df.columns[(df=='Fail').any(axis=0)]
这将为您提供在其行中至少包含一个“失败”的每一列的列名称作为 pandas.Index。 如果必须是列表,只需在末尾添加.tolist()
。
您可以执行包含值检查的列表理解:
[col for col in df.columns if 'Fail' in df[col].values]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.