'Pivot table indexing
I have two lists for columnheaders and rowheaders. I have created a pivot table on the basis of a data frame df so the code is:
df.pivot_table(value = "a", index = "b", columns = "c",aggfunc = "sum").reindex(index=rowheaders)
This works fine but I want to reindex the columns to columnheaders so I was trying:
df.pivot_table(value = "a", index = "b", columns = "c",aggfunc = "sum").reindex(index=rowheaders).reindex(index=columnheaders,axis=1)
but this doesn't work. Please help me out with a solution. Thanks.
Solution 1:[1]
For set columns use axis=1:
.reindex(columnheaders,axis=1)
Or columns parameter:
.reindex(columns=columnheaders)
If need both use:
.reindex(index=rowheaders, columns=columnheaders)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jezrael |
