I have a few questions about JSON in Pandas! I have gotten the data loaded into a dataframe and can search effectively with the query code below. Three questions I have:
- How can I get the individual pieces of 'coord' (i.e. 'coord.lon')
- How do I assign specific columns to variables in the code? (i.e. state = df.loc[df['name'] == city, ['state'])?
- How can I add additional search terms? (i.e. df['name'] == city AND df['state'] == state)
Here is the code I have so far to pull the records I need:
query = df.loc[df['name'] == city, ['id', 'name', 'state', 'coord']]
Here is a sample of the JSON response I am trying to search:
{
"id": 5074329,
"name": "Oakland",
"state": "NE",
"country": "US",
"coord": {
"lon": -96.466972,
"lat": 41.835831
}
},
{
"id": 5074472,
"name": "Omaha",
"state": "NE",
"country": "US",
"coord": {
"lon": -95.93779,
"lat": 41.25861
}
}
As always, THANKS!!
edit: Here is a picture of a couple rows of the dataframe:
url = 'http://bulk.openweathermap.org/sample/city.list.json.gz'
df = pd.read_json(url)
