list_1 = ['22','33','44']
df = {'number' : '22,33,0' , '33' , '22,0'}
any(item in df['number'] for item in list_1)
I want to check whether the any string in dataframe is in the list_1.
CodePudding user response:
There is most likely a better or faster way to do this, but I believe this will give you the results you are expecting
list_1 = ['22','33','44']
df = pd.DataFrame({'number' : ['22,33,0', '33', '22,0', '45']})
df = df.reset_index()
df_index = df
df['number'] = df['number'].str.split(',')
df = df.explode('number')
df['Check'] = np.where(df['number'].isin(list_1), True, False)
df = df.mask(df['Check'] == False).dropna().groupby('index').agg('first').reset_index()
df = pd.merge(df_index, df, how = 'left', left_on = 'index', right_on = 'index', suffixes=('', '_y'))
df[['number', 'Check']].fillna(False)
CodePudding user response:
(df.number.str.split(',').explode().isin(list_1)
.reset_index().groupby('index').any())
number
index
0 True
1 True
2 True