# -*- coding: utf-8 -*-from pandas import Series,DataFrame
import pandas as pd
import numpy as np
defsuoying():'''
返回值:
d1: 一个DataFrame类型数据
'''#s1是Series类型数据
s1=Series(np.random.randn(10),
index=[['a','a','a','b','b','b','c','c','d','d'],[1,2,3,1,2,3,1,2,2,3]])# 请在此添加代码 完成本关任务# ********** Begin *********#
d1=s1.unstack()# ********** End **********## 返回d1return d1
suoying()
Pandas数据取值与选择
第1关 Series数据选择
import pandas as pd
import numpy as np
arr =input()
dates = pd.date_range('20190101', periods=25)# 生成时间序列
df = pd.Series(eval(arr),index=dates)#完成编程要求,并输出结果#********** Begin **********#
df[pd.to_datetime('2019-01-29')]=320
a = df['2019-01-04'::]print(a[a>100])#********** End **********
第2关 DataFrame数据选择方法
import pandas as pd
defdemo(raw_data,origin):
df = pd.DataFrame(raw_data,index=origin)#转换成编程要求所示DataFrame, 并输出#********** Begin **********#print(df.loc[['Florida','Washington'],'deaths':].T)#********** End **********#return
Pandas 进阶
第1关 Pandas 分组聚合
import pandas as pd
import numpy as np
'''
返回最大值与最小值的和
'''defsub(df):######## Begin #######return df.max()- df.min()######## End #######defmain():######## Begin #######
data = pd.read_csv("step1/drinks.csv")
df = pd.DataFrame(data)
mapping ={"wine_servings":sub,"beer_servings":np.sum}print(df.groupby("continent").agg(mapping))######## End #######if __name__ =='__main__':
main()
第2关 Pandas 创建透视表和交叉表
#-*- coding: utf-8 -*-import pandas as pd
#创建透视表defcreate_pivottalbe(data):###### Begin ######return data.pivot_table(index=["day"],values=["tip"],columns=["time"],margins=True,aggfunc=sum)###### End #######创建交叉表defcreate_crosstab(data):###### Begin ######return pd.crosstab(index=[data.day],columns=[data.time],values=data.tip,aggfunc=sum,margins=True)###### End ######defmain():#读取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))if __name__ =='__main__':
main()