Home > Software design >  Pandas dataframe map to a new list of objects
Pandas dataframe map to a new list of objects

Time:11-30

I am new in python, so every tip will be helpful :)

I have a pandas dataframe with multiple columns and I need it converted to a new list of objects. Among all of dataframes columns I have two (lat, lon) that I want in my new object as attributes.

index city lat lon
0 London 42.33 55.44
1 Rome 92.44 88.11

My new list of object will need to look something like this:

[
  {'lat': 42.33, 'lon': 55.44}, 
  {'lat': 92.44, 'lon': 88.11}
] 

More specifically I need this for Machine Learning with ML Studio.

Thanks!

CodePudding user response:

Use Pandas.DataFrame.to_dict(orient) to convert a DataFrame into a dictionary. There are multiple dictionary orientations; for your case use orient='records'

You also want to only select the lat & lon columns, like this:

df[['lat','lon']].to_dict(orient='records')

This will give you your result:

[{'lat': 42.33, 'lon': 55.44}, {'lat': 92.44, 'lon': 88.11}] 

Here are some other orientations you could try out:

dict’ (default) : dict like {column -> {index -> value}}

‘list’ : dict like {column -> [values]}

‘series’ : dict like {column -> Series(values)}

‘split’ : dict like {‘index’ -> [index], ‘columns’ -> [columns], ‘data’ -> [values]}

‘records’ : list like [{column -> value}, … , {column -> value}]

‘index’ : dict like {index -> {column -> value}}

CodePudding user response:

You can choose the columns you want and then use to_dict with orient='records' to get the required result

df[["lat", "lon"]].to_dict(orient='records')

  • Related