I have two Dataframes
df1 = pd.DataFrame(
{
"A": ["1", "3", "22", "43"],
"B": ["6", "19", "4", "31"],
"C": ["47", "15", "8", "19"],
},
index=[0, 1, 2, 3],
)
df2 = pd.DataFrame(
{
"A": ["65", "47", "6", "13"],
"B": ["29", "5", "2", "21"],
"C": ["69", "9", "11", "80"],
},
index=[4, 5, 6, 7],
)
By using pandas, the final result should be:
A B C Ti ID
0 1 6 47 am 01
1 3 19 15 am 01
2 22 4 8 am 01
3 43 31 19 am 01
4 65 29 69 pm 01
5 47 5 9 pm 01
6 6 2 11 pm 01
7 13 21 80 pm 01
I went through Pandas Documentation, and I am trying to merge these two Dataframes by using pd.concat. The code is:
new_df = pd.concat([df1, df2], keys=['am', 'pm']).reset_index()
However, the new Dataframe came out with an extra column level_1 that I don't want it to be there:
level_0 level_1 A B C
0 am 0 1 6 47
1 am 1 3 19 15
2 am 2 22 4 8
3 am 3 43 31 19
4 pm 4 65 29 69
5 pm 5 47 5 9
6 pm 6 6 2 11
7 pm 7 13 21 80
I know reset_index() created that unwanted column. But why?
What else to do to get the same final Dataframe using pandas?