Splitting a Column to Multiple Columns
Suppose we have a dataframe with county names and geolocation as follows:
import pandas as pd
sample_df = pd.DataFrame({ 'County': ['Hancock', 'Stafford', 'Webster'],
'GeoLocation' : ['41.001938375,-83.6665354077',
'38.4207190191,-77.4574003866',
'40.1764332141,-98.4999473753']})
sample_df
County | GeoLocation | |
---|---|---|
0 | Hancock | 41.001938375,-83.6665354077 |
1 | Stafford | 38.4207190191,-77.4574003866 |
2 | Webster | 40.1764332141,-98.4999473753 |
We split the column GeoLocation into multiple using the following operation:
sample_df[['Lat', 'Long']] = sample_df.GeoLocation.str.split(',', n=2, expand=True)
sample_df
County | GeoLocation | Lat | Long | |
---|---|---|---|---|
0 | Hancock | 41.001938375,-83.6665354077 | 41.001938375 | -83.6665354077 |
1 | Stafford | 38.4207190191,-77.4574003866 | 38.4207190191 | -77.4574003866 |
2 | Webster | 40.1764332141,-98.4999473753 | 40.1764332141 | -98.4999473753 |