I have two colums in pandas: df.lat
and df.lon
.
Both have a length of 3897 and 556 NaN values.
My goal is to combine both columns and make a dict out of them.
I use the code:
dict(zip(df.lat,df.lon))
This creates a dict, but with one element less than my original columns.
I used len()
to confirm this. I can not figure out why the dict has one element
less than my columns, when both columns have the same length.
Another problem is that the dict has only raw values, but not the keys "lat" respectively "lon".
Maybe someone here has an idea?
CodePudding user response:
You may have a different length if there are repeated values in df.lat
as you can't have duplicate keys in the dictionary and so these values would be dropped.
A more flexible approach may be to use the df.to_dict()
native method in pandas. In this example the orientation you want is probably 'records'
. Full code:
df[['lat', 'lon']].to_dict('records')