未完
for examples:
example 1:
1 # Code based on Python 3.x 2 # _*_ coding: utf-8 _*_ 3 # __Author: "LEMON" 4 5 import pandas as pd 6 7 d = pd.date_range('20170101', periods=7) 8 aList = list(range(1,8)) 9 10 df = pd.DataFrame(aList, index=d, columns=[' ']) 11 df.index.name = 'value' 12 13 print('----------df.index---------') 14 print(df.index) 15 16 print('---------df.columns---------') 17 print(df.columns) 18 19 print('----------df.values---------') 20 print(df.values) 21 22 print('----------df.describe--------') 23 print(df.describe) 24 25 print('----------information details--------') 26 print(df.head(2)) #获取开始的n条记录 27 print(df.tail(3)) #后去最后的n条记录 28 print(df[3:5]) # df[a:b],获取第a+1至第b-1的记录
运行结果如下:
1 ----------df.index--------- 2 DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04', 3 '2017-01-05', '2017-01-06', '2017-01-07'], 4 dtype='datetime64[ns]', name='value', freq='D') 5 ---------df.columns--------- 6 Index([' '], dtype='object') 7 ----------df.values--------- 8 [[1] 9 [2] 10 [3] 11 [4] 12 [5] 13 [6] 14 [7]] 15 ----------df.describe-------- 16 <bound method NDFrame.describe of 17 value 18 2017-01-01 1 19 2017-01-02 2 20 2017-01-03 3 21 2017-01-04 4 22 2017-01-05 5 23 2017-01-06 6 24 2017-01-07 7> 25 ----------information details-------- 26 27 value 28 2017-01-01 1 29 2017-01-02 2 30 31 value 32 2017-01-05 5 33 2017-01-06 6 34 2017-01-07 7 35 36 value 37 2017-01-04 4 38 2017-01-05 5
example 2:
1 # Code based on Python 3.x 2 # _*_ coding: utf-8 _*_ 3 # __Author: "LEMON" 4 5 from pandas import Series, DataFrame 6 import pandas as pd 7 8 data = {'state': ['Ohino', 'Ohino', 'Ohino', 'Nevada', 'Nevada'], 9 'year': [2000, 2001, 2002, 2001, 2002], 10 'pop': [1.5, 1.7, 3.6, 2.4, 2.9]} 11 12 df = DataFrame(data, index=list(range(1, 6)), 13 columns=['year', 'state', 'pop', 'name']) 14 print(df) 15 16 print('\n', '---------------') 17 print(list(df.ix[3])) 18 19 print('\n', '---------------') 20 print(list(df['year'])) 21 22 aList = ['1', '2', '3', '4'] 23 bList = ['aa', 'bb', 'cb', 'dd'] 24 cList = ['lemon', 'apple', 'orange', 'banana'] 25 26 d = {'num': aList, 'char': bList, 'fruit': cList} 27 28 29 df1 = DataFrame(d, index=['a', 'b', 'c', 'd']) 30 # df2 = DataFrame(bList) 31 print('\n', '---------------') 32 print(df1) 33 #print(df1.num) 34 35 print('\n', '---------------') 36 print(df1.ix['b']) # 获取索引号为 'b' 的行的数据 37 38 39 print('\n', '---------------') 40 print(df1.ix[:2, 1:3]) # 以切片形式获取部分数据
运行结果如下:
1 year state pop name 2 1 2000 Ohino 1.5 NaN 3 2 2001 Ohino 1.7 NaN 4 3 2002 Ohino 3.6 NaN 5 4 2001 Nevada 2.4 NaN 6 5 2002 Nevada 2.9 NaN 7 8 --------------- 9 [2002, 'Ohino', 3.6000000000000001, nan] 10 11 --------------- 12 [2000, 2001, 2002, 2001, 2002] 13 14 --------------- 15 char fruit num 16 a aa lemon 1 17 b bb apple 2 18 c cb orange 3 19 d dd banana 4 20 21 --------------- 22 char bb 23 fruit apple 24 num 2 25 Name: b, dtype: object 26 27 --------------- 28 fruit num 29 a lemon 1 30 b apple 2
example 3 (数据选择-DateFrame.loc()和DateFrame.iloc()) :
1 # Code based on Python 3.x 2 # _*_ coding: utf-8 _*_ 3 # __Author: "LEMON" 4 5 from matplotlib.finance import quotes_historical_yahoo_ochl 6 from datetime import date 7 import pandas as pd 8 9 today = date.today() 10 11 start =(today.year-4, today.month+11, today.day-1) 12 end = (today.year-4, today.month+11, today.day+3) 13 quotes = quotes_historical_yahoo_ochl('AMX', start, end) 14 # each items in quotes is type of "tuple" 15 16 fields = ['date', 'open', 'close', 'high', 'low', 'volume'] 17 18 quotes1 = [] 19 for t in quotes: 20 t1 = list(t) 21 quotes1.append(t1) 22 # each items in quotes1 is type of "list" 23 24 for i in range(0, len(quotes1)): 25 quotes1[i][0] = date.fromordinal(int(quotes1[i][0])) 26 # date format is changed 27 28 df = pd.DataFrame(quotes1, index=range(1, len(quotes1)+1), columns=fields) 29 # df = pd.DataFrame(quotes1, index=['a','b','c','d','e'], columns=fields) 30 # df = df.drop(['date'], axis=1) 31 32 print(df) 33 34 print(df['close'].mean()) #计算某列的mean值 35 # print(dict(df.mean())['close']) #计算某列的mean值 36 37 print(df.sort_values(['open'],ascending = True)) #进行排序,默认(True)是升序 38 print(df[df.open>=21].date) 39 40 41 42 # index是整数 43 print(df.loc[2:5, 'date':'close']) 44 print(df.loc[[2,5],['open','close']]) 45 # loc方法在行和列的选择上是标签形式,可以是连续的选择,或者单个行或列的选择 46 print(df.iloc[1:6,0:4]) #iloc方法以切片形式选取数据 47 48 49 # index是标签形式 50 # print(df.loc['a':'d', 'date':'close']) 51 # print(df.loc[['b','e'],['open','close']]) 52 # loc方法在行和列的选择上是标签形式,可以是连续的选择,或者单个行或列的选择 53 54 # 根据判断条件来选择数据 55 print(df[(df.index>=4) & (df.open>=21)]) 56 57 58 # DateFrame 的均值 59 print(df.mean()) # 默认计算每列的均值 60 print(df.mean(axis=1)) # axis=1是计算每行的均值 61 62 63 ''' 64 # 获取多只股票的信息 65 d1 = (today.year-1, today.month+11, today.day) 66 67 aList = ['BABA', 'KO', 'AMX'] # List of the stock code of companys 68 69 70 for i in aList: 71 q1 = quotes_historical_yahoo_ochl(i, d1, today) 72 df1 = pd.DataFrame(q1) 73 print(df1) 74 '''
运行结果如下:
1 date open close high low volume 2 1 2013-12-03 20.999551 21.156955 21.184731 20.795851 5152600.0 3 2 2013-12-04 20.971773 20.934738 21.064364 20.703261 5174400.0 4 3 2013-12-05 20.518079 20.545857 21.231027 20.379193 7225600.0 5 4 2013-12-06 21.166215 20.601411 21.295841 20.536598 9989500.0 6 20.80974025 7 20.80974025 8 date open close high low volume 9 3 2013-12-05 20.518079 20.545857 21.231027 20.379193 7225600.0 10 2 2013-12-04 20.971773 20.934738 21.064364 20.703261 5174400.0 11 1 2013-12-03 20.999551 21.156955 21.184731 20.795851 5152600.0 12 4 2013-12-06 21.166215 20.601411 21.295841 20.536598 9989500.0 13 4 2013-12-06 14 Name: date, dtype: object 15 16 runfile('E:/Python/Anaco/test_yahoo.py', wdir='E:/Python/Anaco') 17 date open close high low volume 18 1 2013-12-03 20.999551 21.156955 21.184731 20.795851 5152600.0 19 2 2013-12-04 20.971773 20.934738 21.064364 20.703261 5174400.0 20 3 2013-12-05 20.518079 20.545857 21.231027 20.379193 7225600.0 21 4 2013-12-06 21.166215 20.601411 21.295841 20.536598 9989500.0 22 20.80974025 23 date open close high low volume 24 3 2013-12-05 20.518079 20.545857 21.231027 20.379193 7225600.0 25 2 2013-12-04 20.971773 20.934738 21.064364 20.703261 5174400.0 26 1 2013-12-03 20.999551 21.156955 21.184731 20.795851 5152600.0 27 4 2013-12-06 21.166215 20.601411 21.295841 20.536598 9989500.0 28 4 2013-12-06 29 Name: date, dtype: object 30 date open close 31 2 2013-12-04 20.971773 20.934738 32 3 2013-12-05 20.518079 20.545857 33 4 2013-12-06 21.166215 20.601411 34 open close 35 2 20.971773 20.934738 36 5 NaN NaN 37 date open close high 38 2 2013-12-04 20.971773 20.934738 21.064364 39 3 2013-12-05 20.518079 20.545857 21.231027 40 4 2013-12-06 21.166215 20.601411 21.295841 41 date open close high low volume 42 4 2013-12-06 21.166215 20.601411 21.295841 20.536598 9989500.0 43 open 2.091390e+01 44 close 2.080974e+01 45 high 2.119399e+01 46 low 2.060373e+01 47 volume 6.885525e+06 48 dtype: float64 49 1 1.030537e+06 50 2 1.034897e+06 51 3 1.445137e+06 52 4 1.997917e+06 53 dtype: float64