0

I have below dilemma and cannot find any answer online.

I have data in separate rows:

Payment 
Chq 102100
Payment Bank
Payment
Chq 123000

And I need help to combine rows with chq number to its above row like below:

Payment Chq 102100
Payment Bank
Payment Chq 123000

Any suggestions?

RBR
  • 79
  • 3
  • Your data is ambiguous, please provide it as dataframe constructor or dictionary – mozway Oct 14 '21 at 07:58
  • Is [this](https://stackoverflow.com/questions/36392735/how-to-combine-multiple-rows-into-a-single-row-with-pandas) what you're looking for? – ronjafuchs Oct 14 '21 at 08:03

1 Answers1

2

Use a custom group to groupby like below:

# Input data
>>> df

              0
0      Payment 
1    Chq 102100
2  Payment Bank
3       Payment
4    Chq 123000

# Output result
>>> df[0].str.strip() \
         .groupby(df[0].str.contains('^Payment').cumsum()) \
         .agg(' '.join).to_frame()

                    0
0                    
1  Payment Chq 102100
2        Payment Bank
3  Payment Chq 123000
Corralien
  • 109,409
  • 8
  • 28
  • 52