如何通过索引对DataFrame进行分组

时间:2013-05-07 18:37:00

标签: python pandas

现在我有一个DataFrame“df”如下:

In [28]: df[:100]
Out[28]: 
       distkm     modlat     modlon  reallat  reallon         time
0    9.325590  42.423024 -70.512309  42.5040 -70.5419  731800.5514
1    9.286476  42.416112 -70.519175  42.4956 -70.5539  731800.6319
0    4.456535  42.423877 -70.408784  42.4292 -70.4626  731802.0660
1    6.393979  42.405980 -70.367245  42.4297 -70.4382  731802.1556
2    7.447289  42.389719 -70.343267  42.4259 -70.4196  731802.2312
0    4.456535  42.423877 -70.408784  42.4292 -70.4626  731802.0660
1    6.393979  42.405980 -70.367245  42.4297 -70.4382  731802.1556
2    7.447289  42.389719 -70.343267  42.4259 -70.4196  731802.2312
3    7.329755  42.370420 -70.340029  42.4134 -70.4077  731802.3208
4    6.817408  42.355624 -70.337595  42.3942 -70.4021  731802.3972
0     ...
1     ...

我想通过“df.index”分隔DataFrame,如:

     distkm     modlat     modlon  reallat  reallon         time
0    9.325590  42.423024 -70.512309  42.5040 -70.5419  731800.5514
1    9.286476  42.416112 -70.519175  42.4956 -70.5539  731800.6319
     distkm    modlat     modlon   reallat  reallon         time
0    4.456535  42.423877 -70.408784  42.4292 -70.4626  731802.0660
1    6.393979  42.405980 -70.367245  42.4297 -70.4382  731802.1556
2    7.447289  42.389719 -70.343267  42.4259 -70.4196  731802.2312
     distkm    modlat     modlon   reallat  reallon         time
0    4.456535  42.423877 -70.408784  42.4292 -70.4626  731802.0660
1    6.393979  42.405980 -70.367245  42.4297 -70.4382  731802.1556
2    7.447289  42.389719 -70.343267  42.4259 -70.4196  731802.2312
3    7.329755  42.370420 -70.340029  42.4134 -70.4077  731802.3208
4    6.817408  42.355624 -70.337595  42.3942 -70.4021  731802.3972

然后将这些小“df”绘制成一个数字。 我该怎么做? 我试过“groupby(df.index)”,但结果不是我想要的,它只是将每个相同的索引号放在一起。

1 个答案:

答案 0 :(得分:1)

[从评论中迁移]

我对绘图知之甚少,但ISTM你可以按照你想要的方式使用groupby [注意:这假设你的索引由整数而不是字符串组成 - 将0替换为{{ 1}}如果我错了]:

'0'

并且对于每个组,我们可以再次设置索引,例如:

>>> grouped = df.reset_index().groupby(((df.index == 0)*1).cumsum())
>>> for n,g in grouped:
...     print g
...     
   index    distkm     modlat     modlon  reallat  reallon         time
0      0  9.325590  42.423024 -70.512309  42.5040 -70.5419  731800.5514
1      1  9.286476  42.416112 -70.519175  42.4956 -70.5539  731800.6319
   index    distkm     modlat     modlon  reallat  reallon         time
2      0  4.456535  42.423877 -70.408784  42.4292 -70.4626  731802.0660
3      1  6.393979  42.405980 -70.367245  42.4297 -70.4382  731802.1556
4      2  7.447289  42.389719 -70.343267  42.4259 -70.4196  731802.2312
   index    distkm     modlat     modlon  reallat  reallon         time
5      0  4.456535  42.423877 -70.408784  42.4292 -70.4626  731802.0660
6      1  6.393979  42.405980 -70.367245  42.4297 -70.4382  731802.1556
7      2  7.447289  42.389719 -70.343267  42.4259 -70.4196  731802.2312
8      3  7.329755  42.370420 -70.340029  42.4134 -70.4077  731802.3208
9      4  6.817408  42.355624 -70.337595  42.3942 -70.4021  731802.3972