Mike Gacho
Mike Gacho

Reputation: 11

How to split a dataframe in multiple dataframes after a specific column value is found

I have a dataframe of two columns "ExplB" and "remP". Values in remP can be only 0 or 1. I'm trying to split the dataframe in multiple dataframes after the value 1 is met in column remP. How can I do this operation in Python?

How can I solve this? enter image description here

data = {'ExplB':[0,0,0,0.2,0.2,0.15,0,0,0,0,0,0,0,0,0],'remP':[0,0,0,1,0,0,0,0,1,0,0,0,1,0,0]}
df = pd.DataFrame(data, columns = ['ExplB', 'remP'])

Upvotes: 0

Views: 205

Answers (1)

It_is_Chris
It_is_Chris

Reputation: 14113

you can use np.split

# find the index where df['remP']==1
idx = df[df['remP']==1].index

# split your df on that index
dfs = np.split(df, idx)

[   ExplB  remP
 0    0.0     0
 1    0.0     0
 2    0.0     0,    ExplB  remP
 3   0.20     1
 4   0.20     0
 5   0.15     0
 6   0.00     0
 7   0.00     0,     ExplB  remP
 8     0.0     1
 9     0.0     0
 10    0.0     0
 11    0.0     0,     ExplB  remP
 12    0.0     1
 13    0.0     0
 14    0.0     0]

or if you want to split the df after that index then do idx+1

idx = df[df['remP']==1].index
dfs = np.split(df, idx+1)

[   ExplB  remP
 0    0.0     0
 1    0.0     0
 2    0.0     0
 3    0.2     1,    ExplB  remP
 4   0.20     0
 5   0.15     0
 6   0.00     0
 7   0.00     0
 8   0.00     1,     ExplB  remP
 9     0.0     0
 10    0.0     0
 11    0.0     0
 12    0.0     1,     ExplB  remP
 13    0.0     0
 14    0.0     0]

Upvotes: 1

Related Questions