Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ID STR
- 280 11040402-38.58551%;11050101-9.29086%;11070101-52.12363%
- 351 11130203-35%;11130230-65%
- 510 11070103-69%
- 655 11090103-41.63463%;11160102-58.36537%
- 666 11130205-50.00%;11130207-50%
- df['STR'].apply(lambda x: y.split('-') for y in x.split(';'))
- ID STR.LEFT STR.RIGHT
- 280 [11040402, 11050101, 11070101] [38.58551%, 9.29086%, 52.12363%]
- 351 [11130203, 11130230] [35%, 65%]
- 510 [11070103] [69%]
- 655 [11090103, 11160102] [41.63463%, 58.36537%]
- 666 [11130205, 11130207] [50.00%, 50%]
- # set ID as index
- df.set_index('ID', inplace=True)
- new_series = df.STR.str.split(';', expand=True).stack().reset_index(level=-1,drop=True)
- new_df = new_series.str.split('-', expand=True)
- new_df.groupby('ID').agg(list).reset_index()
- ID 0 1
- -- ---- ------------------------------------ --------------------------------------
- 0 280 ['11040402', '11050101', '11070101'] ['38.58551%', '9.29086%', '52.12363%']
- 1 351 ['11130203', '11130230'] ['35%', '65%']
- 2 510 ['11070103'] ['69%']
- 3 655 ['11090103', '11160102'] ['41.63463%', '58.36537%']
- 4 666 ['11130205', '11130207'] ['50.00%', '50%']
- (df['STR'].str.extractall(r'(.*?)-(.*?)(?=;|$)')
- .groupby(level=0)
- .agg(list)
- .set_axis(['STR.LEFT', 'STR.RIGHT'], axis=1, inplace=False))
- STR.LEFT STR.RIGHT
- 0 [11040402, ;11050101, ;11070101] [38.58551%, 9.29086%, 52.12363%]
- 1 [11130203, ;11130230] [35%, 65%]
- 2 [11070103] [69%]
- 3 [11090103, ;11160102] [41.63463%, 58.36537%]
- 4 [11130205, ;11130207] [50.00%, 50%]
- (df['STR'].str.extractall(r'(.*?)-(.*?)(?=;|$)')
- .groupby(level=0)
- .agg(list)
- .set_axis(['STR.LEFT', 'STR.RIGHT'], axis=1, inplace=False)
- .join(df['ID'])
- STR.LEFT STR.RIGHT ID
- 0 [11040402, ;11050101, ;11070101] [38.58551%, 9.29086%, 52.12363%] 280
- 1 [11130203, ;11130230] [35%, 65%] 351
- 2 [11070103] [69%] 510
- 3 [11090103, ;11160102] [41.63463%, 58.36537%] 655
- 4 [11130205, ;11130207] [50.00%, 50%] 666
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement