Go to list of users who liked
Share on X(Twitter)
Share on Facebook
More than 5 years have passed since last update.
"pandas 0.15.2 documentation"の中に、"10 Minutes to pandas"なんてのがあったので、覗いてみたらかなり頭の中が整理された。
まじめにやると10分じゃ終わらんが、便利そうなところだけかいつまんでメモ。
まずはPandasとNumpyのインポート。
# import liblariesimportpandasaspdimportnumpyasnpDataFrameを作る
DataFrameの作成方法も幾つかあるので、その整理。
まずは、DataFrameをnumpyで行列を作り、インデックスとラベルを貼り付けるパターン。
インデックスの作成。
# Create a indexdates=pd.date_range("20130101",periods=6)dates<class'pandas.tseries.index.DatetimeIndex'>[2013-01-01, ..., 2013-01-06]Length: 6, Freq: D, Timezone: NoneDataFrameを作成し、インデックスを貼り付ける。
# Create a DatFramedf=pd.DataFrame(np.random.randn(6,4),index=dates,columns=list("ABCD"))dfABCD2013-01-010.705624-0.7939030.8434250.6726022013-01-02-1.2111292.077101-1.7958610.0280602013-01-030.7060860.3856310.9675680.2718942013-01-042.152279-0.4935761.184289-1.1933002013-01-050.4557670.7875510.2394061.6275862013-01-06-0.639162-0.0526200.288010-2.205777今度は、ラベル別にSeriesを作るイメージでDataFrameを作成。
こっちだと、ラベルごとに別々のdtypesを持てる
df2=pd.DataFrame({'A':1.,'B':pd.Timestamp('20130102'),'C':pd.Series(1,index=list(range(4)),dtype='float32'),'D':np.array([3]*4,dtype='int32'),'E':pd.Categorical(["test","train","test","train"]),'F':'foo'})df2ABCDEF012013-01-0213testfoo112013-01-0213trainfoo212013-01-0213testfoo312013-01-0213trainfoodf2.dtypesAfloat64Bdatetime64[ns]Cfloat32Dint32EcategoryFobjectdtype:objectDataFrameの参照
次は欲しい形でデータを見る方法。
インデックスだけ、columnsだけ、numpyのデータだけ表示。
df.index<class'pandas.tseries.index.DatetimeIndex'>[2013-01-01, ..., 2013-01-06]Length: 6, Freq: D, Timezone: Nonedf.columnsIndex([u'A', u'B', u'C', u'D'], dtype='object')df.valuesarray([[ 0.705624 , -0.79390348, 0.84342517, 0.67260162], [-1.21112884, 2.0771009 , -1.79586146, 0.02806019], [ 0.70608621, 0.38563092, 0.9675681 , 0.27189394], [ 2.15227868, -0.49357565, 1.18428903, -1.19329976], [ 0.45576744, 0.78755094, 0.23940583, 1.62758649], [-0.63916155, -0.05261954, 0.28800958, -2.20577674]])統計量の概要をまとめて表示、これ便利。
df.describe()ABCDcount6.0000006.0000006.0000006.000000mean0.3615780.3183640.287806-0.133156std1.1770661.0345851.0879781.368150min-1.211129-0.793903-1.795861-2.20577725%-0.365429-0.3833370.251557-0.88796050%0.5806960.1665060.5657170.14997775%0.7059710.6870710.9365320.572425max2.1522792.0771011.1842891.627586DataFrameの行列を反転。
df.T2013-01-0100:00:002013-01-0200:00:002013-01-0300:00:002013-01-0400:00:002013-01-0500:00:002013-01-0600:00:00A0.705624-1.2111290.7060862.1522790.455767-0.639162B-0.7939032.0771010.385631-0.4935760.787551-0.052620C0.843425-1.7958610.9675681.1842890.2394060.288010D0.6726020.0280600.271894-1.1933001.627586-2.205777任意の軸でソートをかける。
例えば、ラベルを降順でソート。
df.sort_index(axis=1,ascending=False)DCBA2013-01-010.6726020.843425-0.7939030.7056242013-01-020.028060-1.7958612.077101-1.2111292013-01-030.2718940.9675680.3856310.7060862013-01-04-1.1933001.184289-0.4935762.1522792013-01-051.6275860.2394060.7875510.4557672013-01-06-2.2057770.288010-0.052620-0.639162次はラベル「B」の値で昇順で。
df.sort(columns='B')ABCD2013-01-010.705624-0.7939030.8434250.6726022013-01-042.152279-0.4935761.184289-1.1933002013-01-06-0.639162-0.0526200.288010-2.2057772013-01-030.7060860.3856310.9675680.2718942013-01-050.4557670.7875510.2394061.6275862013-01-02-1.2111292.077101-1.7958610.028060データを選び出す
色んな観点からデータを抜き出してくることが出来ます。
例えば、インデックスの一部だけとか。
ラベルとインデックスを両方指定してデータの抜き出し。
df.loc['20130102':'20130104',['A','B']]AB2013-01-02-1.2111292.0771012013-01-030.7060860.3856312013-01-042.152279-0.493576任意のラベルでグループ作れる。そのままデータ操作できる。
# Creating a DataFramedf=pd.DataFrame({"A":['foo','bar','foo','bar','foo','bar','foo','foo'],"B":['one','one','two','three','two','two','one','three'],"C":np.random.randn(8),"D":np.random.randn(8)})dfABCD0fooone1.1309751.2359401barone-0.140004-2.7149582footwo1.526578-0.1654153barthree-1.049092-0.0374844footwo-1.1823030.2887545bartwo0.5306521.2041256fooone0.678477-0.2733437foothree0.9296240.169822df.sort(columns='B')ABCD2013-01-010.705624-0.7939030.8434250.6726022013-01-042.152279-0.4935761.184289-1.1933002013-01-06-0.639162-0.0526200.288010-2.2057772013-01-030.7060860.3856310.9675680.2718942013-01-050.4557670.7875510.2394061.6275862013-01-02-1.2111292.077101-1.7958610.028060データを選び出す
色んな観点からデータを抜き出してくることが出来ます。
例えば、インデックスの一部だけとか。
ラベルとインデックスを両方指定してデータの抜き出し。
df.loc['20130102':'20130104',['A','B']]AB2013-01-02-1.2111292.0771012013-01-030.7060860.3856312013-01-042.152279-0.493576任意のラベルでグループ作れる。そのままデータ操作できる。
# Creating a DataFramedf=pd.DataFrame({"A":['foo','bar','foo','bar','foo','bar','foo','foo'],"B":['one','one','two','three','two','two','one','three'],"C":np.random.randn(8),"D":np.random.randn(8)})dfABCD0fooone1.1309751.2359401barone-0.140004-2.7149582footwo1.526578-0.1654153barthree-1.049092-0.0374844footwo-1.1823030.2887545bartwo0.5306521.2041256fooone0.678477-0.2733437foothree0.9296240.169822# Grouping and then calculate sumdf.groupby('A').sum()CDAbar-0.658445-1.548317foo3.0833501.255758ピボットテーブルの作成
ピボットテーブルにするためのDataFrameの作成。
# Create a DataFramedf=pd.DataFrame({'A':['one','one','two','three']*3,'B':['A','B','C']*4,'C':['foo','foo','foo','bar','bar','bar']*2,'D':np.random.randn(12),'E':np.random.randn(12)})dfABCDE0oneAfoo0.575699-1.6690321oneBfoo0.592889-2.5261962twoCfoo-2.229949-0.7033393threeAbar0.801380-1.6389834oneBbar-0.135691-0.3025865oneCbar0.3174011.1696086twoAfoo0.064460-0.1094377threeBfoo-0.6050171.0432468oneCfoo-0.3652200.8505359oneAbar1.0335520.22600210twoBbar-0.2605420.35224911threeCbar0.5185311.407827割りと簡単にピボットテーブルに変換できる。
pd.pivot_table(df,values='D',index=['A','B'],columns=['C'])CbarfooABoneA1.0335520.575699B-0.1356910.592889C0.317401-0.365220threeA0.801380NaNBNaN-0.605017C0.518531NaNtwoANaN0.064460B-0.260542NaNCNaN-2.229949まとめ
一度ざっくり眺めておけば、処理に直面したときに返ってこれそうで、すごくありがたいよね。
参考
pandas 0.15.2 documentation
http://pandas.pydata.org/pandas-docs/stable/index.html
10 Minutes to pandas
http://pandas.pydata.org/pandas-docs/stable/10min.html
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme