Home > Software engineering >  pandas: split pandas columns of unequal length list into multiple columns
pandas: split pandas columns of unequal length list into multiple columns

Time:07-21

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

CodePudding user response:

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!

CodePudding user response:

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

  • Related