输入df:
title desc
movie A It is a awesome movie with action
movie B Slow but intense movie.
我要过滤包含以下关键字的行:
keys = ["awesome", "action"]
输出DF:
title desc
movie A It is a awesome movie with action
代码:
index_list = []
for index,rows in df.iterrows():
if any(x in rows["desc"].split(" ") for x in keys) == True:
index_list.append(index)
df = df.loc[index_list]
方法:
In each row, I am checking if any of the keywords are present after splitting the rows
这种方法行之有效,但我很想知道熊猫中是否有一只班轮能够达到同样的效果。
示例:
df.loc[df['column_name'].isin(some_values)]
答案 0 :(得分:3)
是的,为什么-pandas.Series.str.contains
for result in myresult:
user = myresult['user']
pwd = myresult['pwd']
print('{:>5} {:>5}'.format(user, pwd))
答案 1 :(得分:1)
以下应为您解决问题:
>>> import pandas as pd
>>> d = {'title':['movie A', 'movie B'], 'desc':['It is a awesome movie with action', 'Slow but intense movie.']}
>>> df = pd.DataFrame(data=d)
>>> df
desc title
0 It is a awesome movie with action movie A
1 Slow but intense movie. movie B
>>> keys = ["awesome", "action"]
>>> df[df['desc'].str.contains('|'.join(keys))]
desc title
0 It is a awesome movie with action movie A