1

现在我有一个 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)”,但结果不是我想要的,它只是将每个相同的索引号组合在一起。

4

1 回答 1

1

[从评论迁移]

我对绘图知之甚少,但是 ISTM 你可以使用groupby你想要的方式[注意:这假设你的索引由整数组成,而不是字符串——如果我错了,请替换0为]:'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

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

>>> g.set_index("index")
         distkm     modlat     modlon  reallat  reallon         time
index                                                               
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
于 2013-05-07T19:32:37.780 回答