希望在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)
以上就是我的尝试。感谢帮助。干杯!
答案 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')