Travel Tips
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
pandas练习
# 作业1:如图创建时间序列(10*3,值为0-100的随机数),通过索引得到以下值
# ① 索引得到前4行的所有值
# ② 索引得到2017-12-4 12:00:00的数据
# ③ 索引得到2017-12-4 - 2017-12-5的数据
import datetime ts = pd.DataFrame(np.random.rand(10,3)*100, index = pd.date_range('20171201',periods = 10, freq = '12H'), columns = ['value1','value2','value3']) print('创建时间序列为:\n',ts,'\n------') print('前4行的所有值为:\n', ts[:3],'\n------') print('2017-12-4 12:00:00的数据为:\n', ts.loc['2017-12-04 12:00:00'],'\n------') print('2017-12-4 - 2017-12-5的数据为:\n', ts['20171204':'20171205'],'\n------')
ts1 = pd.Series(np.random.rand(10), index = pd.date_range('20170101',periods = 10, freq = 'D')) ts2 = ts1.resample('3D').mean() ts3 = ts1.resample('12H').ffill() print('创建时间序列ts1:\n',ts1,'\n------') print('转换成ts2:\n',ts2,'\n------') print('转换成ts3:\n',ts3,'\n------')
df = pd.DataFrame(np.random.rand(5,2)*100, columns = ['key1','key2']) print('创建Dataframe为:\n',df,'\n------') df['key1'][2] = np.nan df['key1_cumsum'] = df['key1'].cumsum() df['key2_cumsum'] = df['key2'].cumsum() print('df["key1"]的均值为:\n',df['key1'].mean(),'\n------') print('df["key1"]的中位数为:\n',df['key1'].median(),'\n------') print('df["key2"]的均值为:\n',df['key2'].mean(),'\n------') print('df["key2"]的中位数为:\n',df['key2'].median(),'\n------') print('df的累积和为:\n',df,'\n------')
def f(s): s2 = s.unique() if len(s) == len(s2): print('------\n该数组是唯一值数组') else: print('------\n该数组不是唯一值数组') d = input('请随机输入一组元素,用逗号(英文符号)隔开:\n') lst = d.split(',') ds = pd.Series(lst) f(ds) 文本数据 # 作业1:如图创建一个Dataframe,并分别通过字符串常用方法得到3个Series: # ① name字段首字母全部大写 # ② gender字段去除所有空格 # ③ score字段按照-拆分,分别是math,english,art三个学分 df = pd.DataFrame({'name':['jack','tom','Marry','zack','heheda'], 'gender':['M ','M',' F',' M ',' F'], 'score':['90-92-89','89-78-88','90-92-95','78-88-76','60-60-67']}) df['name'] = df['name'].str.capitalize() df['gender'] = df['gender'].str.strip() df['math'] = df['score'].str.split('-', expand=True)[0] df['english'] = df['score'].str.split('-', expand=True)[1] df['art'] = df['score'].str.split('-', expand=True)[2] del df['score'] print('创建Dataframe为:\n',df,'\n------') print('修改后为:\n',df,'\n------')
df1 = pd.DataFrame({'key':['a','b','c'], 'values1':np.random.rand(3)}) df2 = pd.DataFrame({'key':['b','c','d'], 'values2':np.random.rand(3)}) df3 = pd.merge(df1,df2,on='key',how='outer') print('创建df1为:\n',df1,'\n------') print('创建df2为:\n',df2,'\n------') print('合并df3为:\n',df3,'\n------')
df1 = pd.DataFrame({'lkey':['a','b','c'], 'values1':np.random.rand(3)}) df2 = pd.DataFrame({'rkey':['b','c','d'], 'values2':np.random.rand(3)}) df3 = pd.merge(df1,df2,left_on='lkey',right_on='rkey',how='left') print('创建df1为:\n',df1,'\n------') print('创建df2为:\n',df2,'\n------') print('合并df3为:\n',df3,'\n------')
df1 = pd.DataFrame({'key':['a','b','c'], 'values1':np.random.rand(3)}) df2 = pd.DataFrame({'values2':np.random.rand(3), 'values3':[5,6,7]}, index = ['b','c','d']) df3 = pd.merge(df1,df2,left_on='key',right_index=True) print('创建df1为:\n',df1,'\n------') print('创建df2为:\n',df2,'\n------') print('合并df3为:\n',df3,'\n------')
df1 = pd.DataFrame(np.random.rand(4,2), index = list('abcd'), columns = ['values1','values2']) df2 = pd.DataFrame(np.random.rand(4,2), index = list('efgh'), columns = ['values1','values2']) df3 = pd.concat([df1,df2]) print('创建df1为:\n',df1,'\n------') print('创建df2为:\n',df2,'\n------') print('连接df3为:\n',df3,'\n------')
df1 = pd.DataFrame(np.random.rand(4,2), index = list('abcd'), columns = ['values1','values2']) df1['values1'].loc[['b','c']] = np.nan df2 = pd.DataFrame(np.arange(8).reshape(4,2), index = list('abcd'), columns = ['values1','values2']) df3 = df1.combine_first(df2) print('创建df1为:\n',df1,'\n------') print('创建df2为:\n',df2,'\n------') print('df1修补后为:\n',df3,'\n------')
df1 = pd.DataFrame(np.random.rand(4,2), index = list('abcd'), columns = ['values1','values2']) df2 = pd.DataFrame(np.random.rand(4,2), index = list('efgh'), columns = ['values1','values2']) df3 = pd.concat([df1,df2]) print('创建df1为:\n',df1,'\n------') print('创建df2为:\n',df2,'\n------') print('连接df3为:\n',df3,'\n------')
# 作业1:按要求创建Dataframe df,并通过分组得到以下结果
# ① 以A分组,求出C,D的分组平均值
# ② 以A,B分组,求出D,E的分组求和
# ③ 以A分组,得到所有分组,以字典显示
# ④ 按照数值类型分组,求和
# ⑤ 将C,D作为一组分出来,并计算求和
# ⑥ 以B分组,求出每组的均值,求和,最大值,最小值
df = pd.DataFrame({'A' : ['one', 'two', 'three', 'one','two', 'three', 'one', 'two'], 'B' : ['h', 'h', 'h', 'h', 'f', 'f', 'f', 'f'], 'C' : np.arange(10,26,2), 'D' : np.random.randn(8), 'E':np.random.rand(8)}) print('创建df为:\n',df,'\n------') print('以A分组,求出C,D的分组平均值为:\n',df.groupby('A').mean(),'\n------') print('以A,B分组,求出D,E的分组求和为:\n',df.groupby(['A','B']).sum(),'\n------') print('以A分组,筛选出分组后的第一组数据为:\n',df.groupby('A').groups,'\n------') print('按照数值类型分组为:\n',df.groupby(df.dtypes,axis=1).sum(),'\n------') print('将C,D作为一组分出来,并计算求和为:\n',df.groupby({'C':'r','D':'r'},axis=1).sum(),'\n------') print('以B分组,求出每组的均值,求和,最大值,最小值:\n',df.groupby('B').agg([np.mean,np.sum,np.max,np.min]),'\n------')
df = pd.DataFrame({'data1':np.random.rand(8), 'data2':np.random.rand(8), 'key':list('aabbabab')}) print('创建df为:\n',df,'\n------') df_ = df.groupby('key').transform(np.mean) print('求和且合并之后结果为:\n',df.join(df_,rsuffix='_mean'),'\n------')
# 作业1:按要求创建Dataframe df,并通过数据透视表得到以下结果
# ① 以A列聚合,求出C,D的平均值
# ② 以A,B列聚合,求出D,E的均值、求和
# ③ 以B聚合,计算A列的元素频率
df = pd.DataFrame({'A' : ['one', 'two', 'three', 'one','two', 'three', 'one', 'two'], 'B' : ['h', 'h', 'h', 'h', 'f', 'f', 'f', 'f'], 'C' : np.arange(10,26,2), 'D' : np.random.randn(8), 'E':np.random.rand(8)}) print('创建df为:\n',df,'\n------') print('以A列聚合,求出C,D的平均值为:\n',pd.pivot_table(df,values=['C','D'],index='A',aggfunc=np.mean),'\n------') print('以A,B列聚合,求出D,E的均值、求和为:\n',pd.pivot_table(df,values=['D','E'],index=['A','B'],aggfunc=[np.mean,np.sum]),'\n------') print('以B聚合,计算A列的元素频率为:\n',pd.crosstab(df['B'],df['A']),'\n------')
Sed ac lorem felis. Ut in odio lorem. Quisque magna dui, maximus ut commodo sed, vestibulum ac nibh. Aenean a tortor in sem tempus auctor
December 4, 2020 at 3:12 pm
Sed ac lorem felis. Ut in odio lorem. Quisque magna dui, maximus ut commodo sed, vestibulum ac nibh. Aenean a tortor in sem tempus auctor
December 4, 2020 at 3:12 pm
Donec in ullamcorper quam. Aenean vel nibh eu magna gravida fermentum. Praesent eget nisi pulvinar, sollicitudin eros vitae, tristique odio.
December 4, 2020 at 3:12 pm
我是 s enim interduante quis metus. Duis porta ornare nulla ut bibendum
Rosie
6 minutes ago