0

I have a dataframe which i have to split as soon as a specific string value in a column occurs. Ex. df =

    txn_details amt
0   opening_balance 13000
1   opening_balance 15000
2   upi2873         12879
3   upi182y31   12301
4   opening_balance 85050
5   upi79279831 8400

The desired output(3 dataframes)(may vary depending on the no. of occurrences of 'opening_balance'): df_1 =

    txn_details amt
0   opening_balance 13000


df_2 = 
    txn_details amt
0   opening_balance 15000
1   upi2873         12879
2   upi182y31   12301


df_3 = 
    txn_details amt
0   opening_balance 85050
1   upi79279831 8400

I've tried using cumsum() function in pandas but not getting the desired output.

mozway
  • 194,879
  • 13
  • 39
  • 75

1 Answers1

0

Compare opening_balance with txn_details with cumulative sum by Series.cumsum and in dictionary comprehensioncreate dict of DataFrames:

d = {f'df_{i}': g.reset_index(drop=True) 
     for i, g in df.groupby(df['txn_details'].eq('opening_balance').cumsum())}

print (d['df_1'])
       txn_details    amt
0  opening_balance  13000
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252