简体   繁体   English

根据 Pandas DF 中的每行条件获取列标题列表

[英]Getting list of column headers based on condition per row in Pandas DF

I was wondering if it were possible to get a list of column headers based on a condition.我想知道是否可以根据条件获取列标题列表。 For example, if the condition I have is to get a list of the column headers that had a "MATCH" value in each cell, it would output either a list of lists or a list of strings containing the header name, as such:例如,如果我的条件是获取每个单元格中具有“MATCH”值的列标题的列表,则它将 output 列表列表或包含 header 名称的字符串列表,如下所示:

在此处输入图像描述

["a, c", "b, d", "a, b, c, d", "a, d"]
or 
[["a", "c"], ["b", "d"], ["a", "b", "c", "d"], ["a", "d"]]

Thank you for any help!感谢您的任何帮助!

You could try with np.where :您可以尝试使用np.where

import pandas as pd
import numpy as np
df=pd.DataFrame({'a': ['match','mismatch','match'],'b': ['match','match','mismatch'],'c': ['mismatch','mismatch','match']})

print(df)

arr= np.where(df.eq('match'), df.columns, '').sum(axis=1)

print(arr)

Output: Output:

df
          a         b         c
0     match     match  mismatch
1  mismatch     match  mismatch
2     match  mismatch     match

arr
['ab' 'b' 'ac']

And then, to get the desired lists you could try:然后,要获得所需的列表,您可以尝试:

#first option
arr= np.where(df.eq('match'), df.columns, '').sum(axis=1)
arr=list(map(', '.join,arr))
print(arr)

#second option
arr= np.where(df.eq('match'), df.columns, '').sum(axis=1)
arr=list(map(list,arr))
print(arr)

Output: Output:

#first option
['a, b', 'b', 'a, c']

#second option
[['a', 'b'], ['b'], ['a', 'c']]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 pandas DF 中创建新列,每行迭代并将列基于列标题 - Create new column in a pandas DF, iterate per row and based the column on column headers 根据条件更改 pandas df 一行中的值 - Change values in a row of pandas df based on a condition 根据条件向pandas df添加新列 - Adding new column to pandas df based on condition Python-pandas df 行中列表中的字符串到列标题以及计数为值 - Python - Strings in a list in a pandas df row to column headers along with count as values 如何计算每行具有动态条件的 Pandas df 元素(=countif) - How to count Pandas df elements with dynamic condition per row (=countif) Python、Pandas、df 2 部分问题:1. 如何根据特定条件将列添加到列表中 2. 如何从 df 中删除这些列 - Python, Pandas, df 2 part question: 1. how to add a column into a list based of a certain condition 2. how to remove those columns from df python:根据列名条件创建多索引pandas DF - python: Create a multiindex pandas DF based on condition of column names 熊猫:使用iloc根据条件更改df列值 - Pandas: Change df column values based on condition with iloc 根据条件为pandas df列分配三个值 - assign three values based on condition to pandas df column 遍历 DF 列的行并根据条件更改值 - Iterate Through Row of a DF Column and change value based on a condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM