1. 程式人生 > >pandas中Groupby的使用(三)-根據dtype對列進行分組

pandas中Groupby的使用(三)-根據dtype對列進行分組

#-*- coding:utf-8 -*-
import pandas as pd
import numpy as np
df=pd.DataFrame({'key1':['a','a','b','b','a'],'key2':['one','two','one','two','one'],
                 'data1':np.random.randn(5),'data2':np.random.randn(5)})
grouped=df.groupby(df.dtypes,axis=1)
#將這些資料片段做成一個字典
print dict(list(grouped))
#結果:
# {dtype('O'):   key1 key2
# 0    a  one
# 1    a  two
# 2    b  one
# 3    b  two
# 4    a  one, dtype('float64'):       data1     data2
# 0 -0.748004  0.152643
# 1  1.843670  0.963812
# 2  0.080317  0.994834
# 3  0.799884  0.985137
# 4 -0.716374 -0.678516}