I have the following Dataframe:
df:
And I need to create a DataFrame like this:
df2:
I think that it could be done using a pivot table, but im not sure how to do that.
Or there is a better way to accomplish that task?
I have the following Dataframe:
df:
And I need to create a DataFrame like this:
df2:
I think that it could be done using a pivot table, but im not sure how to do that.
Or there is a better way to accomplish that task?
You can use stack
df = df.stack().reset_index().rename(columns={"level_0": 0, "level_1": 1, 0: 2})
print(df)
0 1 2
0 a e 1
1 a f 2
2 a g 3
3 a h 4
4 b e 5
5 b f 6
6 b g 7
7 b h 8
8 c e 9
9 c f 10
10 c g 11
11 c h 12
12 d e 13
13 d f 14
14 d g 15
15 d h 16