简体   繁体   English

如果根据数据帧的行和列值满足特定条件,如何获取列标题?

[英]How to fetch a column header if a particular condition is met based on row and column value of the dataframe?

I have a dataframe like this: 我有一个这样的数据框:

col1    x   y   z
A      yes  no  yes
B      no   no  yes
C      no   yes no
D      yes  no  yes
E      no   no  yes
F      yes  yes no

I would like to select data like this, If my criteria is to find all yes for A , I should get [x,z] , ie the values for A which ever is yes 我想选择这样的数据,如果我的标准是找到A所有yes ,我应该得到[x,z] ,即A的值为yes

If B , [z] C should give [y] 如果B[z] C应给出[y]

What to do? 该怎么办?

First create index by col1 column for indexing by loc , compare by value and last get index values to list : 首先按col1列创建索引以按loc进行索引,按值进行比较,最后获取要list index值:

df = df.set_index('col1')

def get_val(df, idx, val):
    a = df.loc[idx].eq(val)
    return a.index[a].tolist()

print (get_val(df, 'A', 'yes'))
['x', 'z']

print (get_val(df, 'B', 'yes'))
['z']

print (get_val(df, 'C', 'yes'))
['y']

You could use 你可以用

In [499]: df.eq('yes').dot(df.columns)[df.col1.eq('A')]
Out[499]:
0    xz
dtype: object

In [500]: df.eq('yes').dot(df.columns)[df.col1.eq('B')]
Out[500]:
1    z
dtype: object

In [501]: df.eq('yes').dot(df.columns)[df.col1.eq('C')]
Out[501]:
2    y
dtype: object

Here is another one creating a function: 这是另一个创建函数的函数:

df.set_index('col1', inplace=True)

def find_yes(df, x):
    return df.columns[df.loc[x] == 'yes'].tolist()

full example 完整的例子

import pandas as pd

data = '''\
col1    x   y   z
A      yes  no  yes
B      no   no  yes
C      no   yes no
D      yes  no  yes
E      no   no  yes
F      yes  yes no'''

fileobj = pd.compat.StringIO(data)
df = pd.read_csv(fileobj, sep='\s+')

df.set_index('col1', inplace=True)

def find_yes(df, x):
    return df.columns[df.loc[x] == 'yes'].tolist()

print(find_yes(df, 'A'))
print(find_yes(df, 'B'))

Returns: 返回值:

['x', 'z']
['z']

One more option for you - how about using melt then groupby : 为您提供的groupby一种选择-如何使用melt然后使用groupby

from io import StringIO

import pandas as pd

data = StringIO('''col1    x   y   z
A      yes  no  yes
B      no   no  yes
C      no   yes no
D      yes  no  yes
E      no   no  yes
F      yes  yes no''')

df = pd.read_csv(data, sep='\s+')

m = df.melt(id_vars='col1')
matches = m[m['value'] == 'yes'].groupby('col1')\
                                .agg({'variable': list})

this gives the following dataframe: 这给出了以下数据框:

     variable
col1         
A      [x, z]
B         [z]
C         [y]
D      [x, z]
E         [z]
F      [x, y]

暂无
暂无

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

相关问题 如何根据另一列中满足的条件填充 dataframe 列 - How to populate a dataframe column based on condition met in another column Dataframe 如果在 dataframe 列上满足条件,那么在 dataframe 中获取这一行和接下来的 3 行? - Dataframe if a condition is met on a dataframe column then get this row and the next 3 rows in a dataframe? 如果满足条件,则用列名更新行值 - Update row value with column name if condition is met 根据条件替换熊猫数据框列中的数据,如果不满足条件则跳过 - Replacing data in a pandas dataframe column based on condition, skipping if condition not met 根据Pandas中第二列的条件,用另一行的同一列的值填充特定行的列中的值 - Fill values in a column of a particular row with the value of same column from another row based on a condition on second column in Pandas 根据熊猫列中的值从DataFrame中选择特定的行 - Select particular row from a DataFrame based on value in a column in pandas 如何从单独的 DataFrame 中的匹配行值中提取列 header 并基于该列创建新列? - How to extract column header from matching row value in a separate DataFrame and create a new column based off that? 如何根据特定条件填充 dataframe 列中的值 - How to populate values in a column of dataframe based on a particular condition 如何根据 python dataframe 中的特定条件将每个单元格值增加一个特定列 - How to increase a particular column each cell value by one, based on a certain condition in python dataframe 如何在熊猫的where条件下获取特定列的值? - How to fetch value of particular column with where condition in pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM