Let's say I have the following dataframe:
import pandas as pd
import numpy as np
df = pd.DataFrame({'dif_seq': [np.nan, 1, 1, 1, 1, -23, 1, 1, 1, -4, 1, 1], 'data': range(12)})
df
Out[75]:
dif_seq data
0 NaN 0
1 1.0 1
2 1.0 2
3 1.0 3
4 1.0 4
5 -23.0 5
6 1.0 6
7 1.0 7
8 1.0 8
9 -4.0 9
10 1.0 10
11 1.0 11
I would like to split df into a list of dataframes based on the values in df['dif_seq'] as follows (all negative or np.nan values signify the start of a new df):
dif_seq data
0 NaN 0
1 1.0 1
2 1.0 2
3 1.0 3
4 1.0 4
dif_seq data
5 -23.0 5
6 1.0 6
7 1.0 7
8 1.0 8
dif_seq data
9 -4.0 9
10 1.0 10
11 1.0 11
What is the best way to go about this? I have an analagous problem with a very large dataset. So although this is a small example, what would be the fastest route to go?