Reputation: 849
I have pandas dataframe with the following structure:
df1 = pd.DataFrame({'id': 1, 'coords':{0: [(-43.21,-22.15),(-43.22,-22.22)]}})
How can I separate the values from the coords column so that the first item in each list forms the column called latitude and the second the column called longitude, as below?
id| latitude |longitude
1 |(-43.21,-43.22)|(-22.15, -22.22)
Upvotes: 0
Views: 65
Reputation: 56
take the tuple for lat:
lat = [(x[0][0],x[1][0]) for x in df1['coords'].values]
df1['latitude'] = lat
same as for longt:
longt = [(x[0][1],x[1][1]) for x in df1['coords'].values]
df1['longtitude'] = longt
drop coords columns:
df1.drop(columns='coords')
hope this helps!
Upvotes: 0
Reputation: 3634
Simply using the .str
accessor
df1['latitude'] = df1['coords'].str[0]
df1['longitude'] = df1['coords'].str[1]
Time difference:
df1['latitude'] = df1['coords'].str[0]
# 539 µs ± 15.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
df1['latitude'] = df1.coords.apply(lambda x: x[0])
# 624 µs ± 16.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
Upvotes: 0
Reputation: 323266
Using join
with column explode
df1=df1.join(pd.DataFrame(df1.coords.tolist(),index=df1.index,columns=['latitude','longitude']))
Out[138]:
id coords latitude longitude
0 1 [(-43.21, -22.15), (-43.22, -22.22)] (-43.21, -22.15) (-43.22, -22.22)
Upvotes: 1
Reputation: 6132
apply
is a straightforward way:
df1['latitude'] = df1.coords.apply(lambda x: x[0])
df1['longitude'] = df1.coords.apply(lambda x: x[1])
Output:
id coords latitude longitude
0 1 [(-43.21, -22.15), (-43.22, -22.22)] (-43.21, -22.15) (-43.22, -22.22)
Upvotes: 0