前言
github上找的练习pandas
的一个项目,记录下学到了啥。
项目
https://github.com/guipsamora/pandas_exercises
下载到本地用Jupyter notebook打开就好啦。
读数据
众所周知,读取数据直接用pandas.read_(table,csv,sql,excel)
就好,需要注意的是read_table
和read_csv
里默认的参数sep
并不同,前者是\t
,而后者是,
。
计算占比
计算占比时总会agg
两个函数,搞的列名会多一层就很烦:
# 各职业里面男性的占比
def m_num(x):
return x[x.values=='M'].count()
c = users.groupby('occupation').agg({'gender':['count', m_num]}).droplevel(axis=1, level=0)
c['Male ratio'] = c['m_num'] / c['count']
新学到一个思路就是利用value_counts
函数,取两个Series
直接计算:
c = users.groupby('occupation').agg({'gender':m_num}).gender
r = c / users.occupation.value_counts()
div函数
# create a data frame and apply count to gender
gender_ocup = users.groupby(['occupation', 'gender']).agg({'gender': 'count'})
# create a DataFrame and apply count for each occupation
occup_count = users.groupby(['occupation']).agg('count')
# divide the gender_ocup per the occup_count and multiply per 100
occup_gender = gender_ocup.div(occup_count, level = "occupation") * 100
如果是 Series
需要添加参数 axis
。