2022-01-13

Split column into multiple columns when a row starts with a string

I am wanting to split a single column, in a dataframe, into multiple columns whenever a row in the column startswith a specific string. I have a dataframe with a single column like here:

import pandas as pd
lst = ['Group 123 nv-1', 'a, v', 's,b', 'y, i', 'Group  123 mt-d2', 'b, v',
                         'Group  123 id-01', 'n,m', 'x, y', 'z, m', 'l,b']
df = pd.DataFrame(lst, columns=['Group'])
print(df)


              Group
0    Group 123 nv-1
1              a, v
2               s,b
3              y, i
4   Group 123 mt-d2
5              b, v
6   Group 123 id-01
7               n,m
8              x, y
9              z, m
10              l,b

I want to split the Group column into new columns each time that a string starts with "Group 123":

Group 123 nv-1  Group 123 mt-d2  Group 123 id-01
a, v            b, v             n,m
s,b                              x, y
y, i                             z, m
                                 l,b


from Recent Questions - Stack Overflow https://ift.tt/3nmvguC
https://ift.tt/eA8V8J

No comments:

Post a Comment