df:交叉表

import pandas as pd

df = pd.DataFrame({'ame': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
                    'sex': ['Female', 'Male', 'Male', 'Male', 'Female'],
                    'T2DM': [0, 1, 1, 0, 0],
                    'age': [85, 79, 92, 65, 88]})


pd.crosstab(df['sex'], df['age'])
# 输出:
age     65  79  85  88  92
sex                       
Female   0   0   1   1   0
Male     1   1   0   0   1


pd.crosstab(df['sex'], df['T2DM'])
# 输出:
T2DM    0  1
sex         
Female  2  0
Male    1  2

你可能感兴趣的:(DataFrame,pandas,python)