#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

#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

#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