寫在前面的話:
執行個體中的所有資料都是在GitHub上下載下傳的,打包下載下傳即可。
位址是:http://github.com/pydata/pydata-book
還有一定要說明的:
我使用的是Python2.7,書中的代碼有一些有錯誤,我使用自己的2.7版本調通。
# coding: utf-8
from pandas import Series, DataFrame
import pandas as pd
import numpy as np
obj = Series([,,-,])
obj
obj.values
obj.index
obj2 = Series([,,,],index = ['a','b','e','c'])
obj2
obj2.index
obj2['a']
obj2[['a','b','c']]
obj2[obj2>]
obj2 *
np.exp(obj2)
'b' in obj2
'r' in obj2
sdata = {'Ohio':, 'Texas':, 'Oregon':, 'Utah':}
obj3 = Series(sdata)
obj3
states = ['California','Ohio','Oregon','Texas']
obj4 = Series(sdata, index=states)
obj4
pd.isnull(obj4)
pd.notnull(obj4)
obj4.isnull()
obj3 + obj4
obj4.name = 'population'
obj4.index.name = 'state'
obj4
obj.index = ['Bob', 'Steve', 'Jeff', 'Ryan']
obj
data = {'state':['Ohio','Ohio','Ohio','Nevada','Nevada'],'year':[,,,,],'pop':[,,,,]}
frame = DataFrame(data)
frame
DataFrame(data,columns=['year','states','pop'])
frame2 = DataFrame(data, columns=['year','state','pop','debt'],index=['one','two','three','four','five'])
frame2
frame2.columns
frame2['state']
frame2.year
frame2.ix['three']
frame2['debt'] =
frame2
frame2['debt'] = np.arange(.)
frame2
val = Series([-,-,-],index=['two','four','five'])
frame2['debt'] = val
frame2
frame2['eastern'] = frame2.state == 'Ohio'
frame2
del frame2['eastern']
frame2.columns
pop = {'Nevada':{:,:},'Ohio':{:,:,:}}
frame3 = DataFrame(pop)
frame3
frame3.T
DataFrame(pop,index=[,,])
pdata = {'Ohio':frame3.Ohio[:-],'Nevada':frame3.Nevada[:]}
DataFrame(pdata)
frame3.index.name = 'year';frame3.columns.name = 'state'
frame3
frame3.values
frame2.values
obj = Series(range(), index=['a','b','c'])
index = obj.index
index
index[:]
index[] = 'd'
index = pd.Index(np.arange())
obj2 = Series([,-,],index=index)
obj2.index is index
frame3
'Ohio' in frame3.columns
in frame3.index
obj = Series([,,-,],index=['d','b','a','c'])
obj
obj2 = obj.reindex(['a','b','c','d','e'])
obj2
obj.reindex(['a','b','c','d','e'],fill_value=)
obj3 = Series(['bule','purple','yellow'],index=[,,])
obj3.reindex(range(),method='ffill')
frame = DataFrame(np.arange().reshape((,)), index=['a','c','d'],columns=['Ohio','Texas','California'])
frame
frame2 = frame.reindex(['a','b','c','d'])
frame2
states = ['Texas','Utah','California']
frame.reindex(columns=states)
frame.reindex(index=['a','b','c','d'],method='ffill',columns=states)
frame.ix[['a','b','c','d'],states]
obj = Series(np.arange(.),index=['a','b','c','d','e'])
new_obj = obj.drop('c')
new_obj
obj.drop(['d','c'])
data = DataFrame(np.arange().reshape((,)),index=['Ohio','Colorado','Utah','New York'],columns=['one','two','three','four'])
data.drop(['Colorado','Ohio'])
data.drop('two',axis=)
data.drop(['two','four'],axis=)
obj = Series(np.arange(.),index=['a','b','c','d'])
obj['b']
obj[]
obj[:]
obj[['b','a','d']]
obj[{,}]
obj[obj < ]
obj['b':'c']
obj['b':'c'] =
obj
data = DataFrame(np.arange().reshape((,)),index=['Ohio','Colorado','Utah','New York'],columns=['one','two','three','four'])
data
data['two']
data[['three','one']]
data[:]
data[data['three'] > ]
data <
data[data < ] =
data
data.ix['Colorado',['two','three']]
data.ix[['Colorado','Utah'],[,,]]
data.ix[]
data.ix[:'Utah','two']
data.ix[data.three > , :]
s1 = Series([,-,,],index=['a','c','d','e'])
s2 = Series([-,,-,,], index=['a','c','e','f','g'])
s1
s2
s1 + s2
df1 = DataFrame(np.arange(.).reshape((,)),columns=list('bcd'),index=['Ohio','Texas','Colorado'])
df2 = DataFrame(np.arange(.).reshape((,)),columns=list('bde'),index=['Utah','Ohio','Texas','Oregon'])
df1
df2
df1 + df2
df1 = DataFrame(np.arange(.).reshape((,)),columns=list('abcd'))
df2 = DataFrame(np.arange(.).reshape((,)),columns=list('abcde'))
df1
df2
df1 + df2
df1.add(df2, fill_value=)
df1.reindex(columns=df2.columns,fill_value=)
arr = np.arange(.).reshape((,))
arr
arr[]
arr - arr[]
frame = DataFrame(np.arange(.).reshape((,)),columns=list('bde'),index=['Utah','Ohio','Texas','Oregon'])
series = frame.ix[]
frame
series
frame - series
series2 = Series(range(),index=['b','e','f'])
frame + series2
series3 = frame['d']
frame
series3
frame.sub(series3,axis=)
frame = DataFrame(np.random.randn(,),columns=list('bde'),index=['Utah','Ohio','Texas','Oregon'])
frame
np.abs(frame)
f = lambda x:x.max() - x.min()
frame.apply(f)
frame.apply(f,axis=)
def f(x):
return Series([x.min(),x.max()],index=['min','max'])
frame.apply(f)
format = lambda x: '%.2f' % x
frame.applymap(format)
frame['e'].map(format)
obj = Series(range(),index=['d','a','b','c'])
obj.sort_index()
frame = DataFrame(np.arange().reshape((,)),index=['three','one'],columns=['d','a','b','c'])
frame.sort_index()
frame.sort_index(axis=)
frame.sort_index(axis=,ascending=False)
obj = Series([,,-,])
obj.order()
obj = Series([,np.nan,,-,np.nan,])
obj.order()
frame = DataFrame({'b':[,,-,],'a':[,,,]})
frame
frame.sort_index(by='b')
frame.sort_index(by=['a','b'])
obj = Series([,-,,,,,])
obj.rank()
obj.rank(method='first')
obj.rank(ascending=False,method='max')
frame = DataFrame({'b':[,,-,],'a':[,,,],'c':[-,,,-]})
frame
frame.rank(axis=)
obj = Series(range(),index=['a','a','b','b','c'])
obj
obj.index.is_unique
obj['a']
obj['c']
df = DataFrame(np.random.randn(,),index=['a','a','b','c'])
df
df.ix['b']
df = DataFrame([[,np.nan],[,-],[np.nan,np.nan],[.,-]],index=['a','b','c','d'],columns=['one','two'])
df
df.sum()
df.sum(axis=)
df.mean(axis=,skipna=False)
df.idxmax()
df.idxmin()
df.cumsum()
df.describe()
obj = Series(['a','a','b','c']*4)
obj.describe()
import pandas.io.data as web
all_data = {}
for ticker in ['AAPL','IBM','MSFT','GOOG']:
all_data['ticker'] = web.get_data_yahoo(ticker,'1/1/2000','1/1/2010')
price = DataFrame({tic:data['Adj Close'] for tic,data in all_data.iteritems()})
volume = DataFrame({tic:data['Volume'] for tic,data in all_data.iteritems()})
returns = price.pct_change()
returns.tail()
obj = Series([,,,,,,,,])
uniques = obj.unique()
uniques
obj.value_counts()
pd.value_counts(obj.values,sort=False)
mask = obj.isin([])
mask
obj[mask]
data = DataFrame({'Qu1':[,,,,],'Qu2':[,,,,],'Qu3':[,,,,]})
data
result = data.apply(pd.value_counts).fillna()
result
string_data = Series(['aaaaaaa','cccccccc',np.nan,'ddadsds'])
string_data.isnull()
string_data[] = None
string_data.isnull()
from numpy import nan as NA
data = Series([,NA,,NA,])
data.dropna()
data[data.notnull()]
data = DataFrame([[.,,.],[.,NA,NA],[NA,NA,NA],[NA,,.]])
cleaned = data.dropna()
data
cleaned
data.dropna(how='all')
data[] = NA
data
data.dropna(axis=,how='all')
df = DataFrame(np.random.randn(,))
df.ix[:,] = NA;df.ix[:,] = NA
df
df.dropna(thresh=)
df.fillna()
df.fillna({:.,:-})
data = Series(np.random.randn(),index=[['a','a','a','b','b','b','c','c','d','d'],[,,,,,,,,,]])
data
data.index
data['b']
data[:,]
data.unstack()
data.unstack().stack()
frame = DataFrame(np.arange().reshape((,)),index=[['a','a','b','b'],[,,,]],columns=[['Ohio','Ohio','Colorado'],['Green','Red','Green']])
frame
frame.index.names = ['key1','key2']
frame.columns.names = ['state','color']
frame
frame['Ohio']
frame.swaplevel('key1','key2')
frame.sortlevel()
frame.swaplevel(,).sortlevel()
frame.sum(level='key2')
frame.sum(level='color',axis=)