[英]checking if a column contains string from a list, and outputs that string
I'm trying to check a column for a string of a list, and output that string if it's there, and I'm half way so far我正在尝试检查一列列表中的字符串,如果该字符串存在,则为 output,到目前为止我已经完成了一半
k = ['a', 'e','o']
data = pd.DataFrame({"id":[1,2,3,4,5],"word":["cat","stick","door","dog","lung"]})
id word
0 1 cat
1 2 stick
2 3 door
3 4 dog
4 5 lung
I tried this我试过这个
data["letter"] = data['word'].apply(lambda x: any([a in x for a in k]))
trying to get this试图得到这个
id word letter
0 1 cat a
1 2 stick
2 3 door o
3 4 dog o
4 5 lung
but I get this instead但我得到这个
id word letter
0 1 cat True
1 2 stick False
2 3 door True
3 4 dog True
4 5 lung False
You can use built-in next
function with generator expression .您可以将内置的
next
function与生成器表达式一起使用。 The second argument to the next
function is the default , which will be returned if the iterator is exhausted. next
function 的第二个参数是default ,如果迭代器耗尽,将返回它。
data["letter"] = data["word"].apply(
lambda x: next(
(a for a in k if a in x), ""
)
)
Complete code:完整代码:
>>> import pandas as pd
>>>
>>> k = ["a", "e", "o"]
>>> data = pd.DataFrame(
... {
... "id": [1, 2, 3, 4, 5],
... "word": [
... "cat",
... "stick",
... "door",
... "dog",
... "lung",
... ],
... }
... )
>>>
>>> data["letter"] = data["word"].apply(
... lambda x: next(
... (a for a in k if a in x), ""
... )
... )
>>> print(data)
id word letter
0 1 cat a
1 2 stick
2 3 door o
3 4 dog o
4 5 lung
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.