From Tuples To Multiple Columns In Pandas
How do I convert this dataframe location value 0 (Richmond, Virginia, nan, USA) 100
Solution 1:
new_col_list = ['city','state','regions','country']
for n,col in enumerate(new_col_list):
df[col] = df['location'].apply(lambda location: location[n])
df = df.drop('location',axis=1)
Solution 2:
If you return a Series of the (split) location, you can merge (join
to merge on index) the resulting DF directly with your value column.
addr = ['city', 'state', 'region', 'country']
df[['value']].join(df.location.apply(lambda loc: Series(loc, index=addr)))
value city state region country
0100 Richmond Virginia NaN USA
1200 New York City New York NaN USA
Solution 3:
I haven't timed this, but I would suggest this option:
df.loc[:,'city']=df.location.map(lambda x:x[0])
df.loc[:,'state']=df.location.map(lambda x:x[1])
df.loc[:,'regions']=df.location.map(lambda x:x[2])
df.loc[:,'country']=df.location.map(lambda x:x[3])
I'm guessing avoiding explicit for loop might lend itself to a SIMD instruction (certainly numpy looks for that, but perhaps not other libraries)
Post a Comment for "From Tuples To Multiple Columns In Pandas"