gpt4 book ai didi

python - 两个或多个数据框的完全外部连接

转载 作者:行者123 更新时间:2023-11-28 20:01:50 24 4
gpt4 key购买 nike

给定以下三个 Pandas 数据框,我需要将它们合并为类似于 SQL 全外连接。请注意,键是多索引 type_Nid_NN = 1,2,3:

import pandas as pd

raw_data = {
'type_1': [0, 1, 1,1],
'id_1': ['3', '4', '5','5'],
'name_1': ['Alex', 'Amy', 'Allen', 'Jane']}
df_a = pd.DataFrame(raw_data, columns = ['type_1', 'id_1', 'name_1' ])

raw_datab = {
'type_2': [1, 1, 1, 0],
'id_2': ['4', '5', '5', '7'],
'name_2': ['Bill', 'Brian', 'Joe', 'Bryce']}
df_b = pd.DataFrame(raw_datab, columns = ['type_2', 'id_2', 'name_2'])

raw_datac = {
'type_3': [1, 0],
'id_3': ['4', '7'],
'name_3': ['School', 'White']}
df_c = pd.DataFrame(raw_datac, columns = ['type_3', 'id_3', 'name_3'])

预期的结果应该是:

type_1   id_1   name_1   type_2   id_2   name_2   type_3   id_3   name_3
0 3 Alex NaN NaN NaN NaN NaN NaN
1 4 Amy 1 4 Bill 1 4 School
1 5 Allen 1 5 Brian NaN NaN NaN
1 5 Allen 1 5 Joe NaN NaN NaN
1 5 Jane 1 5 Brian NaN NaN NaN
1 5 Jane 1 5 Joe NaN NaN NaN
NaN NaN NaN 0 7 Bryce 0 7 White

如何在 Pandas 中实现这一点?

最佳答案

我建议您让生活不那么复杂,并且不要为您想要合并的事物起不同的名字。

da = df_a.set_index(['type_1', 'id_1']).rename_axis(['type', 'id'])
db = df_b.set_index(['type_2', 'id_2']).rename_axis(['type', 'id'])
dc = df_c.set_index(['type_3', 'id_3']).rename_axis(['type', 'id'])

da.join(db, how='outer').join(dc, how='outer')

name_1 name_2 name_3
type id
0 3 Alex NaN NaN
7 NaN Bryce White
1 4 Amy Bill School
5 Allen Brian NaN
5 Allen Joe NaN
5 Jane Brian NaN
5 Jane Joe NaN

这是获取其他列的令人讨厌的方法

from cytoolz.dicttoolz import merge

i = pd.DataFrame(d.index.values.tolist(), d.index, d.index.names)
d = d.assign(**merge(
i.mask(d[f'name_{j}'].isna()).add_suffix(f'_{j}').to_dict('l')
for j in [1, 2, 3]
))

d[sorted(d.columns, key=lambda x: x.split('_')[::-1])]

id_1 name_1 type_1 id_2 name_2 type_2 id_3 name_3 type_3
type id
0 3 3 Alex 0.0 NaN NaN NaN NaN NaN NaN
7 NaN NaN NaN 7 Bryce 0.0 7 White 0.0
1 4 4 Amy 1.0 4 Bill 1.0 4 School 1.0
5 5 Allen 1.0 5 Brian 1.0 NaN NaN NaN
5 5 Allen 1.0 5 Joe 1.0 NaN NaN NaN
5 5 Jane 1.0 5 Brian 1.0 NaN NaN NaN
5 5 Jane 1.0 5 Joe 1.0 NaN NaN NaN

关于python - 两个或多个数据框的完全外部连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49787325/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com