gpt4 book ai didi

python - Pandas 搜索字符串的特定列

转载 作者:行者123 更新时间:2023-12-04 15:10:11 27 4
gpt4 key购买 nike

我正在尝试搜索我的 DataFrame 中的特定列。如果这些列中的任何一个包含关键字“匹配”,那么我想用"is"写入“结果”列

当前方向

Result Google    Bing    Search
Matched Matched Matched

Matched
Matched

预期结果

Result Google    Bing    Search
Yes Matched Matched Matched
No
Yes Matched
Yes Matched

最佳答案

使用numpy.where测试值由 DataFrame.eqDataFrame.any每行至少测试一个值:

cols = ['Google', 'Bing', 'Search']
df['Result'] = np.where(df[cols].eq('Matched').any(axis=1), 'Yes','No')
print (df)
Result Google Bing Search
0 Yes Matched Matched Matched
1 No NaN NaN NaN
2 Yes Matched NaN NaN
3 Yes NaN Matched NaN

关于python - Pandas 搜索字符串的特定列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65341340/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com