选择包含特定列表值的行值

Selecting row values which contains a specific list values

我有一个示例数据:

df1      df2    df3
Dell.    10.  [intiated, purchased]
Apple    20.  [initiated]
Toshiba. 15.  [purchased]

我想根据 df3 中的值过滤行值。

df3 的过滤示例,当值是 [initiated, purchased] 应该 return

df1      df2    df3
Dell.    10.  [intiated, purchased]

当值 [initiated] 应该 return

时 df3 的示例过滤
df1      df2    df3
Apple.    20.  [intiated]

当值是 [purchased] 时 df3 的过滤示例应该 return

df1      df2    df3
Toshiba.    15.  [purchased]

IIUC 这是一种方法:

df['df3'] = df['df3'].astype(str) # convert to string type if required
req_val =  '[initiated]'
filtered_df = df[df['df3'].eq(req_val)] # use boolean indexing to filter 
输出:
     df1   df2          df3
1  Apple  20.0  [initiated]

完整示例:

df = pd.DataFrame({'df1': {0: 'Dell.', 1: 'Apple', 2: 'Toshiba.'},
 'df2': {0: 10.0, 1: 20.0, 2: 15.0},
 'df3': {0: '[intiated, purchased]', 1: '[initiated]', 2: '[purchased]'}})
req_val =  '[initiated]'
filtered_df = df[df['df3'].eq(req_val)]

其他方式:

val=['intiated','purchased']
#the value that you want to find
m=df['df3'].map(lambda x:all(y in val for y in x))
#check the value 'val' is inside your list or not(it will give you boolean series)

最后过滤掉结果通过m:

df[m]
#OR
df.loc[m]

以上代码的输出:

    df1     df2     df3
0   Dell    10  [intiated, purchased]