函数中,过滤到两个数据帧



我正在熊猫中学习函数并解决问题。我有2个数据帧。我需要通过函数中的条件附加到df

df = pd.DataFrame({ 'Nums':[ ['89' ,'1'], ['89'],['7', '23'],['7', '89', '13','11']], 'Ser': [88858, 54321, 88858, 98547]})
dfPP = pd.DataFrame({ 'Num':[ 5 ,89, 23, 13], 'SerPP': [12345, 54321, 88858, 98547]})
def rule(row,rowPP):
if ((row['Nums'].isin(rowPP['Num'])) & (row['Ser'] == rowPP['SerPP'])).all():
return rowPP['Num']
else:
return 0
df['New'] = df.apply(rule(df, dfPP), axis = 1)

当然它不起作用。我期待着一个新的列与应用的过滤器,例如:

df = pd.DataFrame({ 'Nums':[ ['89' ,'1'], ['89'],['7', '23'],['7', '89', '13','11']], 'Ser': [88858, 54321, 88858, 98547], 'New':[ ['0'], ['89'],['23'],['13']]})

最有效的方法可能是使用列表理解:

df['New'] = [[n] if str(n) in nums else [0]
for nums, n in zip(df['Nums'],
dfPP.set_index('SerPP')['Num'])]

输出:

Nums    Ser   New
0          [89, 1]  88858   [0]
1             [89]  54321  [89]
2          [7, 23]  88858  [23]
3  [7, 89, 13, 11]  98547  [13]

相关内容

  • 没有找到相关文章

最新更新