数据框中几个属性的数据描述

时间:2018-12-02 03:24:58

标签: python python-3.x pandas matplotlib bokeh

假设我有两个属性“性别”和“矮个子或高个子”

df = {'Gender': [Male, Male, Female, Male, Female, Male],
  'Short or Tall':[T, T, S, T, S, S]
  } 

现在,如何获得有关数据框的完整说明

ex:几个高个子和个矮个子的男性及其百分比?

2 个答案:

答案 0 :(得分:3)

使用groupby.size

data = {'Gender': ['Male', 'Male', 'Female', 'Male', 'Female', 'Male'],
          'Short or Tall':['T', 'T', 'S', 'T','S', 'S']}

df = pd.DataFrame(data) # Construct a dataframe.
df = df.groupby(['Gender', 'Short or Tall']).size().unstack(fill_value=0)

print (df)

Short or Tall  S  T
Gender             
Female         2  0
Male           1  3

答案 1 :(得分:1)

使用crosstab

pd.crosstab(df.Gender,df['Short or Tall'])
Out[429]: 
Short or Tall  S  T
Gender             
Female         2  0
Male           1  3