0

Firstly I am pretty new to Python and I'm trying to output the view from a crosstab:

import pandas as pd

Data_18['age_50_flag'] = (Data_18['age'] > 50).astype(int)

pd.crosstab(Data_18.age_50_flag, Data_18.age)

The output however, is being buried:

age 16  17  18  19  20  21  22  23  24  25  ... 93  94  95  96  97  98  99  101 998 999
age_50_flag                                                                                 
0   190 212 224 236 241 256 285 315 354 422 ... 0   0   0   0   0   0   0   0   0   0
1   0   0   0   0   0   0   0   0   0   0   ... 29  36  26  11  10  7   1   1   96  13

Is there a way that I can show all of the output? Any suggestions, please?

Thanks!

  • For printing the max rows/columns of a dataframe refer to https://stackoverflow.com/questions/19124601/pretty-print-an-entire-pandas-series-dataframe – h0r53 Aug 10 '20 at 14:49

1 Answers1

0

assume you have person and age pairs of data then find the individuals whose age is greater than 50 and display the results in a crosstab

age=[16, 17, 20, 24,96]
name=['A','B','C','D','E']
df=pd.DataFrame({'name':name,'age':age})
df['greater50']=df['age'].apply(lambda person: 1 if person>50 else 0)

cross_tab=pd.crosstab(df['name'],df['age'],df['greater50'],aggfunc={'sum'})
print(cross_tab)

output:

sum                    
age    16   17   20   24   96
name                         
A     0.0  NaN  NaN  NaN  NaN
B     NaN  0.0  NaN  NaN  NaN
C     NaN  NaN  0.0  NaN  NaN
D     NaN  NaN  NaN  0.0  NaN
E     NaN  NaN  NaN  NaN  1.0

sns.heatmap(cross_tab)

Golden Lion
  • 3,840
  • 2
  • 26
  • 35