def find_intrastr_column_df(df, column_name, list_of_str_to_find):
"""
Loop in a pandas dataframe, trying to find all rows
that contains the listed words in a particular column and
return a selected dataframe only with the rows founded.
df: Pandas Dataframe
column_name: String. Columns name
list_of_str_to_find: List. List of String of words to find.
"""
list_rows_find = []
for index, row in df.iterrows():
cell_value = row[column_name]
# ignore nan values
if cell_value != cell_value:
break
else:
pass
found = True
for str in list_of_str_to_find:
if str not in cell_value:
found = False
break
else:
pass
if found:
list_rows_find.append(index)
else:
pass
df_selected = df.loc[list_rows_find,:]
return df_selected
import pandas as pd
df = pd.DataFrame({
'path': ['c:/data', 'd:/data', 'e:/data', 'f:/data'],
'description': ['he is a good boy', 'rabin is a good boy', 'he is good', 'he is a boy']
})
df
description path
0 he is a good boy c:/data
1 rabin is a good boy d:/data
2 he is good e:/data
3 he is a boy f:/data
我为你做这个。。。没有测试
让我们一步一步走
认为您的数据框如下:
这就是你得到你想要的东西的方式:
相关问题 更多 >
编程相关推荐