我遇到的问题是我从df_modified['lat'] = df.coordinates.apply(lambda x: x[0])接收时出错,它返回错误TypeError: 'float' object is not subscriptable。因为“coordinates”已经是一个列表(请参见JSON片段),所以我试图使用lambda拉出元素[0],并将其放入名为“lat”的新列中,并将元素[1]放入名为“long”的新列中。如果您能帮助解决这个问题,我们将不胜感激。谢谢您!在import pandas as pd
import json
import requests
from pandas.io.json import json_normalize
# READS IN JSON
source = requests.get('www.url.com')
data = json.loads(source.text)
# Flattens the JSON data since it had nested dictionaries
df = pd.io.json.json_normalize(data)
# Renamed "lat_long.coordinates" because the "." was confusing .apply() function
df.rename(columns={'lat_long.coordinates': 'coordinates'}, inplace=True)
# Created a new data frame with seleted columns
df_modified = df.loc[:, ['county_name', 'arrests', 'incident_count']]
# Attempt to create a new column "lat" and "long" and place the elemnts accordingly i.e. [-75.802503, 41.820569]
df_modified['lat'] = df.coordinates.apply(lambda x: x[0])
df_modified['long'] = df.coordinates.apply(lambda x: x[1])
print(df_modified.head(30))
示例JSON片段
^{pr2}$