Pandas split column values and change into rows

Question:

I have a dataframe like as below

data_df = pd.DataFrame({'email_id': ['[email protected];[email protected]','[email protected];[email protected]','[email protected]','[email protected]','[email protected]','[email protected]','[email protected]'],
             'location': ['ANZ_KOR_ASN','ANZ_KOR','c','IND_GER','e','f','g'],
             'dept_access':[10,11,12,13,14,15,16]})

As you can see there are multiple email-ids and multiple locations in a single row.

I would like to do below

a) Split/explode the rows where there are multiple location values in a single row

b) first split/explode based on location column. Ex: ANZ_ASN_KOR should be split into 3 rows as ANZ, ASN and KOR

I was trying something like below

data_df[["first_loc", "second_loc", "third_loc"]] = data_df['location'].str.split('_'),expand=True)

But this doesn’t help get the output from columns into rows.

I expect my output to be like as below. You can see how the location is split into multiple rows (instead of columns)

enter image description here

Asked By: The Great

||

Answers:

Split to a list and then explode

data_df['location'] = data_df.location.str.split('_')
data_df.explode('location')
Answered By: Vishnudev
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.