user1538798
user1538798

Reputation: 1135

pandas: split pandas columns of unequal length list into multiple columns

I have a dataframe with one column of unequal list which I want to spilt into multiple columns (the item value will be the column names). An example is given below

enter image description here

I have done through iterrows, iterating thruough the rows and examine the list from each rows. It seem workable as my dataframe has few rows. However, I wonder if there is any clean methods

I have done through additional_df = pd.DataFrame(venue_df.location.values.tolist()) However the list break down into as below

enter image description here

thanks fro your help

Upvotes: 0

Views: 196

Answers (2)

INGl0R1AM0R1
INGl0R1AM0R1

Reputation: 1628

First lets explode your location column, so we can get your wanted end result.

s=df['Location'].explode()

Then lets use crosstab in that series so we can get your end result

import pandas as pd
pd.crosstab(s).unstack()

I didnt test it out cause i dont know you base_df

Upvotes: 1

Arihant
Arihant

Reputation: 745

Can you try this code: built assuming venue_df.location contains the list you have shown in the cells.

venue_df['school'] = venue_df.location.apply(lambda x: ('school' in x)+0)
venue_df['office'] = venue_df.location.apply(lambda x: ('office' in x)+0)
venue_df['home'] = venue_df.location.apply(lambda x: ('home' in x)+0)
venue_df['public_area'] = venue_df.location.apply(lambda x: ('public_area' in x)+0)

Hope this helps!

Upvotes: 1

Related Questions