简体   繁体   中英

How to access keys in pandas

How could i access keys in pandas, for example in the following dataframe my key is "d" , i'm trying to use print(df['d']['latitude']) but says key error, when i list the dataframe keys with df.keys() is only "d" , how can i access latitude and longitude?

Index(['_id', 'd'], dtype='object')
0             {'latitude': 35.685, 'longitude': 139.7514}
1                  {'latitude': 60.0, 'longitude': -95.0}
2       {'latitude': 54.94499999999999, 'longitude': -...
3       {'latitude': 41.32740000000001, 'longitude': -...
4       {'latitude': 42.83330000000001, 'longitude': 1...
                              ...
6000         {'latitude': 41.1412, 'longitude': -73.2637}
6001                {'latitude': 52.5, 'longitude': 5.75}
6002    {'latitude': 1.3667000000000087, 'longitude': ...
6003         {'latitude': 41.1412, 'longitude': -73.2637}
6004    {'latitude': 45.16669999999999, 'longitude': 2...
Name: d, Length: 6005, dtype: object

Thanks

If you want to access latitude (for example) for a specific index you may use

df['d'].iloc[index]['latitude']

you should be able to iterate like so

[x['latitude'] for x in df['d'].tolist()]

The best way is to load the json list items to dataframe . A sample code is give below..

import json
from pandas import json_normalize

data = """
            {
            "coordinates":
                    [
                    {'latitude': 35.685, 'longitude': 139.7514},
                    {'latitude': 41.1412, 'longitude': -73.2637},
                    {'latitude': 60.0, 'longitude': -95.0},
                    {'latitude': 52.5, 'longitude': 5.75}
                    ],
            "status": ["ok"]
            }
        """
data_dict = json.loads(data)
df = json_normalize(data_dict["coordinates"])
df

Data Frame will be:

index latitude longitude
0 35.685 139.7514
1 41.1412 -73.2637
2 60.0 -95.0
3 52.5 5.75

if we print latitude only

df2["latitude"]
0 35.6850
1 41.1412
2 60.0000
3 52.5000

Name: latitude, dtype: float64

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM