I have the following dataframe:
import pandas as pd
df = pd.DataFrame({'id': [1,2,3,4],
'point':['(-23.481146, -46.3621371, 0.0)',
'(-22.9441554, -45.4070251, 0.0)',
'(-23.2249286, -45.8056902, 0.0)',
'(-23.23, -45.81)']})
print(df)
id point
1 (-23.481146, -46.3621371, 0.0)
2 (-22.9441554, -45.4070251, 0.0)
3 (-23.2249286, -45.8056902, 0.0)
4 (-23.23, -45.81)
I would like to create two new columns in the dataframe. One to store latitude and one to store longitude, starting from the 'point' column. So I made the following code:
# New columns
cols = ['lat', 'long']
df[cols] = df['point'].apply(lambda p: pd.Series([p[0], p[1]], index=cols))
However, the code is wrong. The output is:
id point lat long
1 (-23.481146, -46.3621371, 0.0) ( -
2 (-22.9441554, -45.4070251, 0.0) ( -
3 (-23.2249286, -45.8056902, 0.0) ( -
4 (-23.23, -45.81) ( -
I would like the output to be:
id point lat long
1 (-23.481146, -46.3621371, 0.0) -23.481146 -46.3621371
2 (-22.9441554, -45.4070251, 0.0) -22.9441554 -45.4070251
3 (-23.2249286, -45.8056902, 0.0) -23.2249286 -45.8056902
4 (-23.23, -45.81) -23.23 -45.81