我有一个包含5列的数据框,我想根据其他4列更新一个列,该数据框看起来像这样
from via to x y
3 2 13 in out
3 2 15 in out
3 2 21 in out
13 2 3
15 2 13
21 2 13
1 12 2
1 12 2
1 12 22
2 12 1 in
2 12 22 in out
22 12 2
想法是根据其他四个列上的值填充X列,其顺序应如下所示:我必须检查x和y是否具有值,如果是,那么我必须使用(from,via)的对应值,并在所有行中将其与(to,via)的值进行比较,如果它们相同,那么我必须将与(从,通过)对应的Y值分配给具有(至,通过)相等值的行的X列因此在此示例中,我可以看到(from = 3,Via = 10)具有x和y值,因此我将采用(from = 3,Via = 10)的值并将其与(to, via),然后我可以在(to = 3,via = 10)
的行中分配(y = out)的值最终结果应为:
from via to x y
3 2 13 in out
3 2 15 in out
3 2 21 in
13 2 3 out
15 2 13 out
21 2 13
1 12 2 out
1 12 2 out
1 12 22 out
2 12 1 in
2 12 22 in out
22 12 2 out
我如何在熊猫数据框中执行此操作?
我找不到完全相同的结果,但是我使用了所描述的算法:
# identify the lines where a change will occur and store the index and the new value
tmp = df.assign(origix=df.index).merge(df[~df['x'].isna() & ~df['y'].isna()],
left_on = ['from', 'via'], right_on = ['to', 'via'],
suffixes=('_x', '')).set_index('origix')
# apply changes in dataframe:
df.loc[tmp.index, 'x'] = tmp['y']
它给出: