Niels Henkens
Niels Henkens

Reputation: 2696

extra dict values from dataframe column with empty values

I have a dataframe that has been made from JSON-data. The problem I have is that two columns contained dictionaries. I managed to extract the values from one of those columns with an iterator, but the other column also contains some empty cells which cause an error.

This is what (part of) my dataframe looks like:

    area                                           latLng     price
0   191.0  {u'latitude': 52.000000, u'longitude': 5.220000}   120000
1   192.0  {u'latitude': 52.080000, u'longitude': 5.240000}   420000
2   140.0  {u'latitude': 52.100000, u'longitude': 5.230000}   175000
3   180.0                                             None    165000
...

(I edited the lat/lng values for privacy-reasons)

The problem lies with the latLng column. I want to get the latitude and longitude in different columns so that I can easily use the location.

I tried the following piece of code for another similar column and that worked fine. But the latLng column contains some empty cells which cause problems:

df["lat"] = [d.get('latitude') for d in df.latLng]
df["lon"] = [d.get('longitude') for d in df.latLng]

AttributeError: 'NoneType' object has no attribute 'get'

I also tried to get it working with some kind of if statement, but I don't seem to get that working.

df["lat"] = [d.get('latitude') for d in df.latLng if d.notnull()]

AttributeError: 'dict' object has no attribute 'notnull'

Can someone offer some help on how to solve this problem?

Upvotes: 1

Views: 405

Answers (1)

Peter Leimbigler
Peter Leimbigler

Reputation: 11105

You can add a condition in your list comprehension as follows. Where latlng is None, lat and lon end up as NaN.

df['lat'] = [d.get('latitude') if d is not None else None for d in df.latlng]
df['lon'] = [d.get('longitude') if d is not None else None for d in df.latlng]

Upvotes: 2

Related Questions