如何交换包含列表且大小不同的 2 pandas 数据帧中的行?

How to swap rows in 2 pandas dataframes which contain lists and have different size?

我有两个 Pandas 数据帧,df1 和 df2。每个数据框都有一列名为 'Path'。每行都有一个列表。他们是这样的:

df1

Path
[OAK, ORD, FLL, PBG]
[OAK, SEA, FLL, PBG]
[OAK, AUS, FLL, PBG]
[OAK, LAS, FLL, PBG]
[OAK, LAX, FLL, PBG]
[OAK, DAL, FLL, PBG]
[OAK, MDW, FLL, PBG]
[OAK, BWI, FLL, PBG]

df1 构造函数是:

{'Path': {0: ['OAK', 'ORD', 'FLL', 'PBG'],   2: ['OAK', 'SEA', 'FLL', 'PBG'],   4: ['OAK', 'AUS', 'FLL', 'PBG'],   6: ['OAK', 'LAS', 'FLL', 'PBG'],   8: ['OAK', 'LAX', 'FLL', 'PBG'],   10: ['OAK', 'DAL', 'FLL', 'PBG'],   12: ['OAK', 'MDW', 'FLL', 'PBG'],   14: ['OAK', 'BWI', 'FLL', 'PBG']}}

df2

Path
[OAK, DFW, FLL, PBG]
[OAK, JFK, FLL, PBG]
[OAK, MCI, FLL, PBG]
[OAK, PHX, FLL, PBG]
[OAK, DEN, FLL, PBG]
[OAK, HOU, FLL, PBG]
[OAK, ATL, FLL, PBG]

df2 构造函数是:

{'Path': {1: ['OAK', 'DFW', 'FLL', 'PBG'], 3: ['OAK', 'JFK', 'FLL', 'PBG'], 5: ['OAK', 'MCI', 'FLL', 'PBG'], 7: ['OAK', 'PHX', 'FLL', 'PBG'], 9: ['OAK', 'DEN', 'FLL', 'PBG'], 11: ['OAK', 'HOU', 'FLL', 'PBG'], 13: ['OAK', 'ATL', 'FLL', 'PBG']}}

一个问题是我的数据框中的行数不同。我想将 df1 每一行的第二个元素与 df2 每一行的第二个元素交换。如果没有相应的行,则不应修改或删除该行。期望的输出是:

df1

Path
[OAK, DFW, FLL, PBG]
[OAK, JFK, FLL, PBG]
[OAK, MCI, FLL, PBG]
[OAK, PHX, FLL, PBG]
[OAK, DEN, FLL, PBG]
[OAK, HOU, FLL, PBG]
[OAK, ATL, FLL, PBG]
[OAK, BWI, FLL, PBG]

df2

Path
[OAK, ORD, FLL, PBG]
[OAK, SEA, FLL, PBG]
[OAK, AUS, FLL, PBG]
[OAK, LAS, FLL, PBG]
[OAK, LAX, FLL, PBG]
[OAK, DAL, FLL, PBG]
[OAK, MDW, FLL, PBG]

如何在 Python 中完成?

你可以在这里使用combine_first()将一系列列表转换成数据框:

n=pd.DataFrame(df2['Path'].tolist())
m=pd.DataFrame(df1['Path'].tolist())
#----------------------------------------------------
df1_final=n[[1]].combine_first(m).dropna().agg(list,1)
df2_final=m[[1]].combine_first(n).dropna().agg(list,1)

print(df1_final)
print('\n')
print(df2_final)

0    [OAK, DFW, FLL, PBG]
1    [OAK, JFK, FLL, PBG]
2    [OAK, MCI, FLL, PBG]
3    [OAK, PHX, FLL, PBG]
4    [OAK, DEN, FLL, PBG]
5    [OAK, HOU, FLL, PBG]
6    [OAK, ATL, FLL, PBG]
7    [OAK, BWI, FLL, PBG]
dtype: object


0    [OAK, ORD, FLL, PBG]
1    [OAK, SEA, FLL, PBG]
2    [OAK, AUS, FLL, PBG]
3    [OAK, LAS, FLL, PBG]
4    [OAK, LAX, FLL, PBG]
5    [OAK, DAL, FLL, PBG]
6    [OAK, MDW, FLL, PBG]
dtype: object