I have a nested hearders in a dataframe after pivoting it and I’d like to make it flat, with repeated item on each rows (like in a excel) May you help me ?
JavaScript
x
24
24
1
import pandas as pd
2
3
data = {
4
"year": [2022, 2022 , 2021 , 2021 , 2020 ],
5
"client": ["A", "B", "B", "C", "C"],
6
"product" : [ "P1", "P2" , "P1", "P2", "P1"],
7
"sales" : [ 10,20, 20, 22, 25]
8
9
}
10
11
df = pd.DataFrame(data)
12
13
df2 = df.pivot_table ( index = ["year","client"] , columns = ["product"] , values = ["sales"] , aggfunc ="sum")
14
df2
15
16
sales
17
product P1 P2
18
year client
19
2020 C 25.0 NaN
20
2021 B 20.0 NaN
21
C NaN 22.0
22
2022 A 10.0 NaN
23
B NaN 20.0
24
where I’d like to get a flat header and reapeated rows :
JavaScript
1
7
1
year client P1 P2
2
2020 C 25.0 NaN
3
2021 B 20.0 NaN
4
2021 C NaN 22.0
5
2022 A 10.0 NaN
6
2022 B NaN 20.0
7
many thanks
Nico
Advertisement
Answer
You need to use droplevel(0)
which will remove the top level (Sales). Setting columns.name
to None
will remove the Products
. So, after df2
is created, add these lines…
JavaScript
1
12
12
1
>> df2.columns = df2.columns.droplevel(0)
2
>> df2.columns.name = None
3
>> df2 =df2.reset_index()
4
>> df2
5
6
year client P1 P2
7
0 2020 C 25.0 NaN
8
1 2021 B 20.0 NaN
9
2 2021 C NaN 22.0
10
3 2022 A 10.0 NaN
11
4 2022 B NaN 20.0
12