简体   繁体   English

为关键字过滤 dataframe 列,从找到每个关键字的行中返回单独的列值(名称)

[英]Filter a dataframe column for a keyword, return seperate column value (name) from the row where each keyword is found

if a have a data frame and I want to return the values in one column if I find a keyword in another.如果有一个数据框,并且如果我在另一列中找到关键字,我想返回一列中的值。 So below if I search for apple I want the output to be [a,b]所以下面如果我搜索apple ,我希望 output 是[a,b]

like this:像这样:

names words
a     apple
b     apple
c     pear

I would want a list that is: [a,b]我想要一个列表: [a,b]

I have found ways to return the boolean value using str.contains , but not sure how to take the value from another column in the same row which will give me the name.我找到了使用str.contains返回 boolean 值的方法,但不确定如何从同一行中的另一列中获取值,这将为我命名。 There must be a post I cant find if anyone can direct me there.一定有一个帖子我找不到如果有人可以指导我那里。

You could do你可以做

list(df[df['words'].str.contains('apple')]['names'])

resulting in导致

['a', 'b']
  1. df['words'].str.contains('apple') build a boolean pandas series for the condition df['words'].str.contains('apple')为条件构建一个 boolean pandas 系列
  2. the series resulting from previous line is used filter the original dataframe df上一行产生的系列用于过滤原始 dataframe df
  3. in the dataframe resulting from previous line, the 'names' column is selected在上一行产生的 dataframe 中,选择了“名称”列
  4. in the dataframe resulting from previous line, the column is cas to a list在上一行产生的 dataframe 中,该列是一个列表

Full code:完整代码:

import io
import pandas as pd
data = """
names words
a     apple
b     apple
c     pear
"""
df = pd.read_csv(io.StringIO(data), sep='\s+')

lst = list(df[df['words'].str.contains('apple')]['names'])


>>>print(lst)

['a', 'b']

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

相关问题 从表中返回列名,在该表中使用python和pandas在任何行中都找到了特定值 - return the column name from a table where a specific value in any row were found with python and pandas 在pandas Dataframe的每一行中搜索一个字符串,并返回找到的列名称 - Search a string in each row of a pandas Dataframe and return the column names where found 用单独的 dataframe 中的匹配值替换列名 - Replace column name with matching value in seperate dataframe 返回新列中每一行中第一个匹配值的列名 - Return column name of the first matching value in each row in a new column 在随时间变化的概率数据框中返回第一列名称,其中每行的值 &lt;.5 - In the data frame of probabilities over time return first column name where value is < .5 for each row 关键字数据框的Python列解析器 - Python Column Parser for Keyword Dataframe 如何将数据框单元格中的关键字分别转换为自己的列 - How to convert keyword in cell of dataframe to own column each 熊猫数据框返回列标题链接到每一行的数据值 - Pandas dataframe return column header linked to data value for each row 为数据框中的每一行收集值为 True 的列名 - Collect the column name whose value is True for each row in dataframe 在一个 Pandas DataFrame 中找到每一行第二大值的列名 - Find the column name of the second largest value of each row in a Pandas DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM