[英]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']
df['words'].str.contains('apple')
build a boolean pandas series for the condition df['words'].str.contains('apple')
为条件构建一个 boolean pandas 系列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.