I have a dataframe with a MultiIndex column with two levels like this:
import pandas as pd
df = pd.DataFrame(
np.arange(16).reshape(4,4),
columns=pd.MultiIndex.from_tuples(
(("ID1", "Field1"), ("ID1", "Field2"), ("ID2", "Field1"), ("ID2", "Field2"),))
)
df.insert(0, "Date", pd.bdate_range("2021-11-01", "2021-11-04"))
df
Date ID1 ID2
Field1 Field2 Field1 Field2
0 2021-11-01 0 1 2 3
1 2021-11-02 4 5 6 7
2 2021-11-03 8 9 10 11
3 2021-11-04 12 13 14 15
I am trying to convert level 0 of the MultiIndex column to another column called "ID" so that the dataframe looks like this:
df1 = pd.DataFrame(
np.arange(16).reshape(8,2),
columns=["Field1", "Field2"]
)
df1.insert(0, "ID", ["ID1", "ID2"]*4)
df1.insert(0, "Date", pd.bdate_range("2021-11-01", "2021-11-04").repeat(2))
df1
Date ID Field1 Field2
0 2021-11-01 ID1 0 1
1 2021-11-01 ID2 2 3
2 2021-11-02 ID1 4 5
3 2021-11-02 ID2 6 7
4 2021-11-03 ID1 8 9
5 2021-11-03 ID2 10 11
6 2021-11-04 ID1 12 13
7 2021-11-04 ID2 14 15
I tried using the melt function
df.melt(col_level=0, id_vars=["Date"])
but that combines Field1 and Field2 into a single column instead of leaving them as separate columns as desired.
Any suggestions would be appreciated. Thank you!