I am trying to pivot on a dataframe that looks like so
country col_a col_b col_c status group
a 4 5 6 confirmed z
a 4 5 6 failed z
a 4 5 6 unknown y
a 4 5 6 confirmed z
b 4 5 6 failed y
b 4 5 6 confirmed y
b 4 5 6 failed z
b 4 5 6 confirmed z
b 4 5 6 confirmed z
I am trying to pivot so that I have a total for each country, and then each group within that country is broken down. As below.
country group confirmed failed unknown
a NaN 2 1 1
Nan z 2 1 0
NaN y 0 0 1
b NaN 3 2 0
NaN z 2 1 0
NaN y 1 1 0
The issue i'm having is that whilst it will look just like this, it will then append the other cols across the top and just repeat the status as below.
col_a col_b col_c
country group confirmed failed unknown confirmed failed unknown confirmed failed unknown
a NaN 2 1 1 2 1 1 2 1 1
Nan z 2 1 0 2 1 0 2 1 0
NaN y 0 0 1 0 0 1 0 0 1
b NaN 3 2 0 3 2 0 3 2 0
NaN z 2 1 0 2 1 0 2 1 0
NaN y 1 1 0 1 1 0 1 1 0
The code im using is -
testdf = df2.pivot_table(index=['country','group'], columns='status', aggfunc=len, fill_value=0)
and when it prints in the console, it looks fine. But as soon as I output to excel, its all broken!
Any ideas?