Quantcast
Viewing all articles
Browse latest Browse all 14126

how can i add the total values in pivot_table using margins

df2 = pd.DataFrame(pd.read_excel(r"titanic.xls"))result = df2.pivot_table(index=['pclass', 'survived'], aggfunc='size')print(result)

Result| pclass | survived | || -------- | -------------- | ---------|| 1| 0 | 123|| | 1 | 200|| 2| 0 | 158|| | 1 | 119|| 3| 0 | 528|| | 1 | 181|

the above code is simple its just that i wanted to get the total on every "pclass" "survived" and in my research i just need to use margins=True in pivot_table()

df2.pivot_table(index=['pclass', 'survived'], aggfunc='size', margins=True)

Like this but when i run it i encountered an error

AttributeError: 'Series' object has no attribute 'columns'

i tried to make the excel into dataframe in a different line but its still the same


Viewing all articles
Browse latest Browse all 14126

Trending Articles