pandasでgroupbyしたオブジェクトの中身をみよう

groupbyオブジェクトをprintする方法

groupbyしたあとは「DataFrameGroupBy」オブジェクトになる。その中身をみるには以下のような方法がある

 df = pd.DataFrame([{"a" : 1,"b" : 2 , "c" : 3 },{"a" : 2 , "b" : 3, "c" : 5},{"a" : 1 , "b" : 9, "c" : 5} ] ,index=['1','2','3'])
# df
   a  b  c
1  1  2  3
2  2  3  5
3  1  9  5
 df2 = df.groupby('a')
# df2
<pandas.core.groupby.generic.DataFrameGroupBy object at 0x114688588>
# df2.first()
   b  c
a
1  2  3
2  3  5
# df2.last()
   b  c
a
1  9  5
2  3  5
>>> for key, item in df2:
...     print(df2.get_group(key), "\n\n")
...
   b  c
1  2  3
3  9  5


   b  c

※ pandas インデックスオブジェクトは重複値をサポートしています。groupby操作でグループキーとして一意ではないインデックスを使用した場合、同じインデックス値に対するすべての値が1つのグループ内にあるとみなされるため、集約関数の出力には一意なインデックス値のみが含まれることになります。

Group by: split-apply-combine — pandas 1.1.3 documentation