基于另一个数据框架列值创建一个新的数据框架列



我有两个数据帧df1df2

no  plan  current flag
0  abc1   249       30  Y/U
1  abc2   249       30    N
2  abc3   249       30  Y/D

plan  offer
0     149     20
1     249     30
2     349     40

我想在df1中添加一个额外的列,这样如果df1['flag'] == 'Y/U',它将搜索df2['offer']中比较df1['current']的下一个更高的数字。同样,同样的规则也适用于较低的数字,其中标志是Y/D。(如果标志为N,则保持不变))

预期输出:

no  plan  current flag   Pos
0  abc1   249       30  Y/U   40
1  abc2   249       30    N   30
2  abc3   249       30  Y/D   20

我试着用apply来做。

df1['pos'] = (df1.apply(lambda x: next((z for (y, z) in zip(df2['plan'], df2['offer'])
if y > x['plan'] if z > x['current']), None), axis=1))

但它给出的结果考虑了每一种情况'Y/U'。

不使用plan,您就可以达到这样的预期结果。你可以直接用列表。

offers = df2['offer'].sort_values().tolist()
def assign_pos(row, offers):
index = offers.index(row['current'])
if row['flag'] == "N":
row['pos'] = row['current']
elif row['flag'] == 'Y/U':
row['pos'] = offers[index + 1]
elif row['flag'] == 'Y/D':
row['pos'] = offers[index - 1]

return row
df1 = df1.apply(assign_pos, args=[offers], axis=1)

最新更新