If Row In Dataframe Starts With Keyword, Concat It With The Row Above
I have a question similar to here but I have not been able to break through it. I have a DataFrame structured like this: 0 inner join xx 1 on xx 2 and xx 3 and yy 4 and aa 5 inner
Solution 1:
You can use groupby
+apply
:
(df.groupby((~df['join'].str.startswith('and ')).cumsum())
['join'].apply(' '.join)
)
output:
join
1 inner join xx
2 on xx and xx and yy and aa
3 inner join zz
Name: join, dtype: object
Solution 2:
You can solve this using a simple for
loop:
result = []
for j in df['join']:
if j.startswith('and') and len(result) > 0:
result[-1] += ' ' + j
else:
result.append(j)
Post a Comment for "If Row In Dataframe Starts With Keyword, Concat It With The Row Above"