import pandas as pd
import numpy as np
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 |
# 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
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 |