Travel Tips
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
pandas练习
import numpy as np import pandas as pd # 作业1:分别由字典、数组的方式,创建以下要求的Series dic = {'Jack':90,'Tom':89,'Marry':92,'Zack':65} ar = np.array([90,89,92,76]) print('由字典创建为:\n',pd.Series(dic, dtype = np.float, name = '作业1'),'\n------') print('由数组创建为:\n',pd.Series(ar, index = ['Jack','Tom','Marry','Zack'],dtype = np.float, name = '作业1'),'\n------')
# 作业1:创建一个Series,包含10个元素,且每个值为0-100的均匀分布随机值,index为a-j,请分别筛选出:
# ① 标签为b,c的值为多少
# ② Series中第4到6个值是哪些?
# ③ Series中大于50的值有哪些?
s = pd.Series(np.random.rand(10)*100, index = list('abcdefghij')) print('创建Series为:\n', s, '\n------') print('标签为b,c的值为:\n', s['b'], s['c'],'\n------') print('Series中第4到6个值为:\n', s[3:6],'\n------') print('Series中大于50的值为:\n', s[s>50],'\n------')
Pandas数据结构Series:基本技巧
s = pd.Series(np.arange(10), index = list('abcdefghij')) print('创建s:\n',s,'\n------') s[['a','e','f']] = 100 s.drop('b', inplace=True) print('s修改后:\n',s,'\n------')
s1 = pd.Series(np.random.rand(5)*10,index = list('abcde')) s2 = pd.Series(np.random.rand(5)*10,index = list('cdefg')) print('创建s1为:\n',s1,'\n') print('创建s2为:\n',s2,'\n------') print('s1+s2为:\n',s1+s2,'\n------')
df1 = pd.DataFrame({'one':[1,2,3,4,5], 'two':[2,3,4,5,6], 'three':[3,4,5,6,7], 'four':[4,5,6,7,8]}, index = list('abcde')) df2 = pd.DataFrame({'one':pd.Series([1,2,3,4,5],index = list('abcde')), 'two':pd.Series([2,3,4,5,6],index = list('abcde')), 'three':pd.Series([3,4,5,6,7],index = list('abcde')), 'four':pd.Series([4,5,6,7,8],index = list('abcde'))}) df3 = pd.DataFrame(np.arange(20).reshape(5,4), index = list('abcde'), columns = ['one','two','three','four']) df4 = pd.DataFrame([{'one':1,'two':2,'three':3,'four':4}, {'one':2,'two':3,'three':4,'four':5}, {'one':3,'two':4,'three':5,'four':6}, {'one':4,'two':5,'three':6,'four':7}, {'one':5,'two':6,'three':7,'four':8}], index = list('abcde')) print('结果Dataframe为:\n',df4)
# 作业1:如图创建Dataframe(4*4,值为0-100的随机数),通过索引得到以下值
# ① 索引得到b,c列的所有值
# ② 索引得到第三第四行的数据
# ③ 按顺序索引得到two,one行的值
# ④ 索引得到大于50的值
df = df1 = pd.DataFrame(np.random.rand(16).reshape(4,4)*100, index = ['one','two','three','four'], columns = ['a','b','c','d']) print('创建Dataframe为:\n',df,'\n-------') print('b,c列的所有值为:\n', df[['b','c']],'\n------') print('第三第四行的值为:\n', df[2:4],'\n------') print('按顺序索引得到two,one行的值为:\n', df.loc[['two','one']],'\n------') print('大于50的值为:\n', df[df>50],'\n------')
df = pd.DataFrame(np.random.rand(3,3)*100, index = ['a','b','c'], columns = ['v1','v2','v3']) print('创建Dataframe为:\n',df,'\n-------') print('按照index降序:\n',df.sort_index(ascending = False),'\n-------') print('按照第二列值大小降序:\n',df.sort_values(['v2'], ascending = False),'\n-------')
df1 = pd.DataFrame(np.random.rand(5,2)*100, index = ['a','b','c','d','e'], columns = ['v1','v2']) print('创建Dataframe为:\n',df1,'\n-------') df2 = df1.T.drop('e',axis = 1) df2['b'] = 100 print('修改后为:\n',df2,'\n-------')
import datetime print('1、请输出当前时间:', datetime.datetime.now(),'\n------') print('2、请输出时间:', datetime.datetime(2017,5,1,12,30),'\n------') print('3、请输出时间:', datetime.datetime(2000,12,1),'\n------')
datelst = [] for i in range(1,32): datelst.append('2017-12-%i' %i) print('创建时间列表为:\n',datelst,'\n------') t = pd.to_datetime(datelst) print('转化成DatetimeIndex为:\n',t,'\n------') n = (len(t) + 1)/2 - 1 print('月中日期为:\n',t[int(n)],'\n------')
ts1 = pd.Series(np.random.rand(5), index = pd.date_range('20170101',periods = 5)) ts2 = pd.Series(np.random.rand(4), index = pd.date_range('2017','2018',freq = '3M')) ts3 = pd.DataFrame(np.random.rand(4,4), index = pd.date_range('20171201',periods = 4, freq = '10T'), columns = ['value1','value2','value3','value4']) print('时间序列1:\n',ts1,'\n------') print('时间序列2:\n',ts2,'\n------') print('时间序列3:\n',ts3,'\n------')
ts1 = pd.Series(np.random.rand(5), index = pd.date_range('20170501 12:0:0',periods = 5, freq = '10T')) ts2 = ts1.asfreq('5T','ffill') print('创建时间序列ts1:\n',ts1,'\n------') print('转换成ts2:\n',ts2,'\n------')
ts1 = pd.Series(np.random.rand(5), index = pd.period_range('20170101','20170501',freq = 'M')) ts2 = pd.Series(np.random.rand(5), index = pd.period_range('20170101',periods = 5,freq = '2H')) print('时间序列1:\n',ts1,'\n------') print('时间序列2:\n',ts2,'\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