【发布时间】:2023-02-02 20:56:43
【问题描述】:
我有以下数据框 (df),其中包含“文本”列中的字符串:
| text | sth |
|---|---|
| abdcdtext1wrew | ... |
| qwerqdtext2cvufu | ... |
| iuotext3tvbv | ... |
| iuotvbvewre | ... |
我也有一个系列(df_look_for) 包含我要查找的字符串:
| look_for |
|---|
| text1 |
| text2 |
| text3 |
我的目标是检查“文本" 列是否包含 " 中的字符串之一寻找" 列。如果它包含我想将找到的字符串添加为 df 中的新列。例如:
| text | sth | found_str |
|---|---|---|
| abdcdtext1wrew | ... | text1 |
| qwerqdtext2cvufu | ... | text2 |
| iuotext3tvbv | ... | text3 |
| iuotvbvewre | ... | NaN |
到目前为止,我正在尝试使用str.contains(),但还没有成功。
任何帮助将不胜感激!
【问题讨论】:
-
您的预期输出与您的
df不匹配。此行中缺少一行。 -
对不起,我修好了。
标签: python string dataframe substring