How can I concatenate two pandas dataframes, where one dataframe has multiindexed columns? I need to preserve the multiindex in the final dataframe.
import numpy as np
import pandas as pd
df1_cols = ["a", "b"]
df1_vals = np.random.randint(1, 10, [2, 2])
df1 = pd.DataFrame(data=df1_vals, columns=df1_cols)
df2_cols = pd.MultiIndex.from_tuples([("c", "1"), ("c", "2"), ("d", "1"), ("d", "2")])
df2_vals = np.random.randint(1, 10, [2, 4])
df2 = pd.DataFrame(data=df2_vals, columns=df2_cols)
df = pd.concat([df1, df2], axis=1)
Using pd.concat(), the multiindex will be squashed.
a b (c, 1) (c, 2) (d, 1) (d, 2)
0 3 7 1 6 1 3
1 6 1 2 7 6 3