这有效:

import pandas as pd

raw_data = {
        'type_1': [1, 1],
        'id_1': ['2', '3'],
        'name_1': ['Alex', 'Amy']}
df_a = pd.DataFrame(raw_data, columns = ['type_1', 'id_1', 'name_1'])

raw_datab = {
        'type_2': [1, 1],
        'id_2': ['4', '5'],
        'name_2': ['Billy', 'Brian']}
    df_b = pd.DataFrame(raw_datab, columns = ['type_2', 'id_2', 'name_2'])

    dfs = [df_a.set_index(['type_1','id_1']),
           df_b.set_index(['type_2','id_2'])]

    df = pd.concat(dfs, axis=1)
    print (df)


印刷品:

     name_1 name_2
1 2    Amy    NaN
  3   Alex    NaN
  4    NaN  Billy
  5    NaN  Brian


如果更改以下内容,则它将不起作用,因为raw_data中的多索引键是重复的:

     raw_data = {
        'type_1': [1, 1],
        'id_1': ['2', '2'],   #  <-- changed from 3 to 2
        'name_1': ['Alex', 'Amy']}


以及以下内容:

raw_datab = {
        'type_2': [1, 1],
        'id_2': ['2', '5'], #  <-- changed from 4 to 2
        'name_2': ['Billy', 'Brian']}


结果,AlexAmyBilly都具有相同的多索引键[1,2],因此concat失败并显示:


  无法处理非唯一的多索引!


但是重复的数据是有效的,无论如何我都需要将其串联起来。这是我需要实现的结果(请注意,这应该是外部联接,默认情况下):

     name_1  name_2
1 2    Amy    Billy
  2   Alex    Billy
  5    NaN    Brian


熊猫怎么可能?

最佳答案

axis=1更改为axis=0(默认)

df = pd.concat(dfs)
df
Out[52]:
            name_1 name_2
type_1 id_1
1      2      Alex    NaN
       2       Amy    NaN
       4       NaN  Billy
       5       NaN  Brian

根据您的评论..
df_a.merge(df_b,left_on=['type_1','id_1'],right_on=['type_2','id_2'],how='outer').set_index(['type_2','id_2']).drop(['type_1','id_1'],1)
Out[80]:
            name_1 name_2
type_2 id_2
1      2      Alex  Billy
       2       Amy  Billy
       5       NaN  Brian

关于python - 需要处理具有非唯一多索引的串联数据帧,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49786769/

10-12 19:31