Reputation: 837
I want to split the following data into two columns, latitude
and longitude
and put them in a dataframe.
0 (45.349586099999996, -75.81031967988278)
1 (-37.77922725, 175.2010323246593)
2 (-42.9945669, 170.7100413)
3 (-39.2711067, 174.154795)
4 (51.2800275, 1.0802533)
5 (-41.30222105, 172.89453190955697)
6 (-35.3712702, 173.7405337)
7 (-45.7255555, 168.2936808)
8 (-40.3284102, 175.8190684)
9 (-45.1299859, 169.5248818)
10 (-37.9503756, 176.93828736155422)
Can anyone help me please?
Upvotes: 0
Views: 276
Reputation: 5648
Another way:
data='''a b
0 (45.349586099999996, -75.81031967988278)
1 (-37.77922725, 175.2010323246593)
2 (-42.9945669, 170.7100413)
3 (-39.2711067, 174.154795)
4 (51.2800275, 1.0802533)
5 (-41.30222105, 172.89453190955697)
6 (-35.3712702, 173.7405337)
7 (-45.7255555, 168.2936808)
8 (-40.3284102, 175.8190684)
9 (-45.1299859, 169.5248818)
10 (-37.9503756, 176.93828736155422)'''
df = pd.read_csv(io.StringIO(data), sep=' \s+', engine='python')
df[['lat', 'lon']] = df.b.str[1:-1].str.split(',', expand=True)
a b lat lon
0 0 (45.349586099999996, -75.81031967988278) 45.349586099999996 -75.81031967988278
1 1 (-37.77922725, 175.2010323246593) -37.77922725 175.2010323246593
2 2 (-42.9945669, 170.7100413) -42.9945669 170.7100413
3 3 (-39.2711067, 174.154795) -39.2711067 174.154795
4 4 (51.2800275, 1.0802533) 51.2800275 1.0802533
5 5 (-41.30222105, 172.89453190955697) -41.30222105 172.89453190955697
6 6 (-35.3712702, 173.7405337) -35.3712702 173.7405337
7 7 (-45.7255555, 168.2936808) -45.7255555 168.2936808
8 8 (-40.3284102, 175.8190684) -40.3284102 175.8190684
9 9 (-45.1299859, 169.5248818) -45.1299859 169.5248818
10 10 (-37.9503756, 176.93828736155422) -37.9503756 176.93828736155422
Upvotes: 3
Reputation: 26676
Data
Position
0 (45.349586099999996,-75.81031967988278)
1 (-37.77922725,175.2010323246593)
2 (-42.9945669,170.7100413)
3 (-39.2711067,174.154795)
4 (51.2800275,1.0802533)
5 (-41.30222105,172.89453190955697)
6 (-35.3712702,173.7405337)
7 (-45.7255555,168.2936808)
8 (-40.3284102,175.8190684)
9 (-45.1299859,169.5248818)
10 (-37.9503756,176.93828736155422)
Solution
#Strip of the brackets if column is string and not tuple.
#str.split column to make it a list
#stack it to dataframe it
pd.DataFrame(np.vstack(df['Position'].str.strip('\(\)').str.split(',')), columns=['Lat','Long'])
Lat Long
0 45.349586099999996 -75.81031967988278
1 -37.77922725 175.2010323246593
2 -42.9945669 170.7100413
3 -39.2711067 174.154795
4 51.2800275 1.0802533
5 -41.30222105 172.89453190955697
6 -35.3712702 173.7405337
7 -45.7255555 168.2936808
8 -40.3284102 175.8190684
9 -45.1299859 169.5248818
10 -37.9503756 176.93828736155422
Upvotes: 1