Split Combined Lat/Long Coordinate Variables Into Seperate Variables In Pandas

Want to learn more? I recommend these Python books: Python for Data Analysis, Python Data Science Handbook, and Introduction to Machine Learning with Python.

Preliminaries

```import pandas as pd
import numpy as np
```

Create an example dataframe

```raw_data = {'geo': ['40.0024, -105.4102', '40.0068, -105.266', '39.9318, -105.2813', np.nan]}
df = pd.DataFrame(raw_data, columns = ['geo'])
df
```
geo
0 40.0024, -105.4102
1 40.0068, -105.266
2 39.9318, -105.2813
3 NaN

Split the geo variable into seperate lat and lon variables

```# Create two lists for the loop results to be placed
lat = []
lon = []

# For each row in a varible,
for row in df['geo']:
# Try to,
try:
# Split the row by comma and append
# everything before the comma to lat
lat.append(row.split(',')[0])
# Split the row by comma and append
# everything after the comma to lon
lon.append(row.split(',')[1])
# But if you get an error
except:
# append a missing value to lat
lat.append(np.NaN)
# append a missing value to lon
lon.append(np.NaN)

# Create two new columns from lat and lon
df['latitude'] = lat
df['longitude'] = lon
```

View the dataframe

```df
```
geo latitude longitude
0 40.0024, -105.4102 40.0024 -105.4102
1 40.0068, -105.266 40.0068 -105.266
2 39.9318, -105.2813 39.9318 -105.2813
3 NaN NaN NaN