如何在列中搜索某些文本字符串并输出某个单词?

时间:2017-11-30 13:35:19

标签: python pandas

希望在pandas dataframe列中搜索多个文本字符串。找到某个字符串后,程序应输出某个字。

import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile

df = pd.read_excel(OriginalFile.xlsx', sheet_name='Sheet1')
category = []

for row in df['THINGS']:

    if df[df['THINGS'].str.contains('APPLE', na=False)]:
        category.append('RED')
    elif df[df['THINGS'].str.contains('GRASS', na=False)]:
        category.append('GREEN')
    else:
        category.append('NoCat')

df['category'] = category
print(df)

以上就是我的尝试。感谢帮助。干杯!

1 个答案:

答案 0 :(得分:0)

我认为你需要numpy.select

m1 = df['THINGS'].str.contains('APPLE', na=False)
m2 = df['THINGS'].str.contains('GRASS', na=False)
df['GRASS'] = np.select([m1, m2],['RED','GREEN'], default='NoCat')