RBR
RBR

Reputation: 79

Combine two row datas into one based on Condition using Pandas

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?

Upvotes: 0

Views: 74

Answers (1)

Corralien
Corralien

Reputation: 120409

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

Upvotes: 2

Related Questions