Pandas, split dataframe by monotonic increase of column value
<p>I have a gigantic dataframe with a datetime type column called time, and another float type column called dist, the data frame is sorted based on time, and dist already. 
I want to split the dataframe into several dataframes base on monotonic increase of dist.</p>

<p>Split</p>

<pre><code>   dt                    dist
0  20160811 11:10        1.0
1  20160811 11:15        1.4
2  20160811 12:15        1.8
3  20160811 12:32        0.6
4  20160811 12:34        0.8
5  20160811 14:38        0.2
</code></pre>

<p>into</p>

<pre><code>   dt                    dist
0  20160811 11:10        1.0
1  20160811 11:15        1.4
2  20160811 12:15        1.8

   dt                    dist
0  20160811 12:32        0.6
1  20160811 12:34        0.8

   dt                    dist
0  20160811 14:38        0.2
</code></pre>

<p>You can calculate a difference vector of <code>dist</code> column and then do a <code>cumsum()</code> on the condition <code>diff &lt; 0</code> (this creates a new id whenever the <code>dist</code> decreases from previous value)</p>

<pre><code>df['id'] = (df.dist.diff() &lt; 0).cumsum()

print(df)

#               dt  dist  id
#0  20160811 11:10   1.0   0
#1  20160811 11:15   1.4   0
#2  20160811 12:15   1.8   0
#3  20160811 12:32   0.6   1
#4  20160811 12:34   0.8   1
#5  20160811 14:38   0.2   2

for _, g in df.groupby((df.dist.diff() &lt; 0).cumsum()):
    print(g)

#               dt  dist
#0  20160811 11:10   1.0
#1  20160811 11:15   1.4
#2  20160811 12:15   1.8
#               dt  dist
#3  20160811 12:32   0.6
#4  20160811 12:34   0.8
#               dt  dist
#5  20160811 14:38   0.2
</code></pre>

python pandas numpy dataframe