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?
CodePudding user response:
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