3 回答

TA貢獻1851條經(jīng)驗 獲得超4個贊
使用apply, 和' '.join, 然后使用列表推導來獲取匹配的值
此外,您必須使用axis=1它才能工作:
print(df.apply(lambda x: ' '.join([i for i in x['Col1'].split() if i in x['Col2'].split()]), axis=1))
輸出:
0 the cat
1
2 chicken
dtype: object
如果你想要NULL,而不僅僅是一個空值,請使用:
print(df.apply(lambda x: ' '.join([i for i in x['Col1'].split() if i in x['Col2'].split()]), axis=1).str.replace('', 'NULL'))
輸出:
0 the cat
1 NULL
2 chicken
dtype: object

TA貢獻1783條經(jīng)驗 獲得超4個贊
這里不需要使用 lambda 函數(shù),只需檢查每個單詞是否包含在同一列的字符串中。zip() 函數(shù)對于列迭代非常有用。這是一種方法:
import pandas as pd
data_frame = pd.DataFrame(
{'col1':{
1:'the cat crossed a road',
2:'the dog barked',
3:'the chicken barked',},
'col2':{
1: 'the cat alligator',
2: 'some words here',
3: 'chicken soup'}}
)
# output the overlap as a list
output = [
[word for word in line1.split() if word in line2.split()]
for line1, line2 in zip(data_frame['col1'].values, data_frame['col2'].values)
]
# To add your new values a column
data_frame['col3'] = output
# Or, if desired, keep as a list and remove empty rows
output = [row for row in output if row]

TA貢獻1877條經(jīng)驗 獲得超6個贊
檢查
l=[' '.join([t for t in x if t in y]) for x, y in zip(df1.Col1.str.split(' '),df2.Col2.str.split(' '))]
pd.DataFrame({'Col3':l})
Out[695]:
Col3
0 the cat
1
2 chicken
添加回答
舉報