Search code examples
pythonpandasdataframemulti-index

Concat two dataframes and create multi-index from indexes


I have two DataFrames df_1 and df_2 are:

df_1 = pd.DataFrame({"A1":"1", "A2":"2", "A3":"3"}, index=[2411])
df_1.index.name = "i_1"
df_2 = pd.DataFrame({"B1":"4", "B2":"5", "B3":"6"}, index=[123122])
df_2.index.name = "i_2"

I want to concat them, so the final DataFrames will look like:

                A1  A2  A3  B1  B2  B3
i_1     i_2                     
2411    123122  1   2   3   4   5   6

Basicly, that's concatination along axis 1 and mooving the setting a multi-index from indexes.

The most closest to the desired result, that i have done is:

df_1 = df_1.reset_index()
df_2 = df_2.reset_index()
df_f = pd.concat([df_1,df_2], axis=1)
df_f = pd.DataFrame(df_f, index=pd.MultiIndex.from_arrays([float(df_1["i_1"]), float(df_2["i_2"])], names=["i_1","i_2"]))
del df_f["i_1"]
del df_f["i_2"]

But the result is:

A1  A2  A3  B1  B2  B3
i_1 i_2                     
2411.0  123122.0    NaN NaN NaN NaN NaN NaN

Solution

  • I think simpliest is reset_index of both df for default indexes, so concat align data nice and last set_index:

    df_f = pd.concat([df_1.reset_index(),df_2.reset_index()], axis=1).set_index(['i_1','i_2'])
    print (df_f)
                A1 A2 A3 B1 B2 B3
    i_1  i_2                     
    2411 123122  1  2  3  4  5  6
    

    In you solution is problem different indexes, so after concat get 2 rows, because data cannot allign (not same indexes):

    df_f = pd.concat([df_1,df_2], axis=1)
    print (df_f)
             A1   A2   A3   B1   B2   B3
    2411      1    2    3  NaN  NaN  NaN
    123122  NaN  NaN  NaN    4    5    6
    

    Then get NaNs because in DataFrame constructor create new Multiindex but data not aligne again - in original df_f are data size (2x6) and want assign to 1,6 structure, also indexes are different.