头歌:Pandas分组聚合与透视表的创建

第1关 Pandas分组聚合

import pandas as pd
import numpy as np


# 返回最大值与最小值的和
def sub(df):
    ########## Begin #########
    result=df.max()-df.mean()
    ########## End #########
def jicha(arr):
    result=arr.max()-arr.min()
    return result
# 得到目标DataFrame
def main():
    ########## Begin #########
    df=pd.read_csv("step1/drinks.csv")
    result=df.groupby(by="continent").agg({"wine_servings":jicha,"beer_servings":np.sum})

    ########## End #########
    return result

第2关 Pandas创建透视表和交叉表 

#-*- coding: utf-8 -*-
import pandas as pd

#创建透视表
def create_pivottalbe(data):
    ########## Begin ##########
    df = data.pivot_table(index=['day'],columns=['time'],values=['tip'],aggfunc=sum,margins=True)
    return df  


    ########## End ##########

#创建交叉表
def create_crosstab(data):
    ########## Begin ##########
    df1 = pd.crosstab(index=data['day'],columns=data['time'],values=data['tip'],aggfunc=sum,margins=True)
    return df1 
    ########## End ##########

def main():
    #读取csv文件数据并赋值给data
    ########## Begin ##########

    data= pd.read_csv('step2/tip.csv')
    ########## End ##########
    piv_result = create_pivottalbe(data)
    cro_result = create_crosstab(data)
    print("透视表:\n{}".format(piv_result))
    print("交叉表:\n{}".format(cro_result))


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