答案 0 :(得分:1)
用Series.str.split
检查DataFrame.isin
的第一列的值,将第二列的值平分,并用DataFrame.any
每行至少获得一个True
值,传递给{ {3}}(带有过滤器第一列),并在必要时通过boolean indexing
创建一个列Dataframe
:
df = pd.DataFrame({'column_a':['ga lt','ka','ku','na ma',np.nan, np.nan],
'column_b':['se','ga','ma po','na','ka ch', 'wa wo']})
vals = [y for x in df['column_b'] for y in x.split()]
mask = df['column_a'].str.split(expand=True).isin(vals).any(axis=1)
df = df.loc[mask, 'column_a'].to_frame('column_a_in_column_b')
print (df)
column_a_in_column_b
0 ga lt
1 ka
3 na ma