You are on page 1of 1

Pandas crosstab explained

Source dataframe Basic Usage

pd. c r os st ab( df . mak e, df . body _st yl e)

Margin Totals Label Rows and Columns

pd. cr oss t ab( df . make, df . num_door s , pd. c r os st ab( df . mak e, df . body _st yl e,
mar gi ns=Tr ue, r ownames=[ ' Aut o Manuf act ur er ' ] ,
mar gi ns_name=" Tot al " ) col names=[ ' Body St y l e' ] )

Grouping and Aggregating Values

pd. cr os st ab( df . mak e, [ df . body _s t yl e, df . dr i v e_wheel s] , val ues=df . cur b_wei ght , aggf unc=' mean' ) . f i l l na( ' - ' )

Average curb weight for all


fwd toyota wagons

Normalize All Values Normalize Rows Normalize Columns


pd. cr osst ab( df . mak e,
pd. c r os st ab( df . mak e, pd. cr oss t ab( df . mak e,
df . body _s t y l e,
df . body _s t yl e, df . body _s t y l e,
nor mal i ze=Tr ue) nor mal i ze=' i ndex ' ) nor mal i z e=' c ol umns ' )

Practical Business Python pbpython.con/pandas-crosstab.html

You might also like