Home > Mobile >  Extracting values to new columns with pandas
Extracting values to new columns with pandas

Time:02-24

I have a dataframe where the coordinates column comes in this format

[-7.821, 37.033]

I would like to create two columns where the first is lonand the second is lat

I've tried

my_dict = df_map['coordinates'].to_dict()
df_map_new = pd.DataFrame(list(my_dict.items()),columns = ['lon','lat']) 

But the dictionary that is created does not split the values between , Instead it creates a dict with the following format

0: '[-7.821, 37.033]'

What is the best way to extract the values within [,] and put them into two new columns in the original dataframe df_map?

Thank you in advance!

CodePudding user response:

You can parse string:

pattern = r"\[(?P<lon>.*),\s*(?P<lat>.*)\]"
out = df_map['coordinates'].str.extract(pattern).astype(float)
print(out)

# Output
     lon     lat
0 -7.821  37.033

CodePudding user response:

Convert values to lists by ast.literal_eval, then to lists instead dicts:

import ast

my_L = df_map['coordinates'].apply(ast.literal_eval).tolist()
df_map_new = pd.DataFrame(my_L,columns = ['lon','lat']) 

CodePudding user response:

Additionally to the answers already provided, you can also try this:

ser_lon = df['coordinates'].apply(lambda x: x[0])
ser_lat = df['coordinates'].apply(lambda x: x[1])

df_map['lon'] = ser_lon
df_map['lat'] = ser_lat

  • Related