-
-
Notifications
You must be signed in to change notification settings - Fork 400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
用Python如何解决嵌套(list)的问题读取地区(area)的数据? #67
Comments
你好,由于不同省份的城市数量不定,所以丁香园和我都是选择了在 在数据挖掘过程中,载入数据必定会碰到类似的问题,大部分的数据都不是可以直接载入的,所以我下面的内容可能不止针对这一个问题,而是针对类似的问题的一个解决方案。 事实上,这个返回数据中的 大致的数据结构如下: "results": [
{
"cities": [
{
... # 这个城市内各种参数
},
... # 其他城市的字典
],
... # 这个省份的其他变量
},
... # 其他省份
] 因此,如果希望能够解析这个数据,需要从最底层的字典列表开始逐层往上展开,即先考虑读取 读取
|
另外,可以参考数据仓库的script.py文件,里面的 这份代码仅包括了对 |
你好, import pandas as pd
import os
import numpy
import requests
url = 'https://lab.isaaclin.cn/nCoV/api/area?latest=1'
r = requests.request('GET', url)
data = r.json()
df = pd.DataFrame.from_records(data['results'])
from datetime import datetime
df data = pd.DataFrame()
temp = pd.DataFrame(r.json()['results'][0]['cities'])
# # 其他变量也可以通过此方法处理
temp['province_confirmedCount'] = r.json()['results'][0]['confirmedCount']
# data = pd.DataFrame()
from pandas import concat
for province in r.json()['results']:
temp = pd.DataFrame(province['cities'])
temp['province_confirmedCount'] = province['confirmedCount']
data = pd.concat([data, temp], ignore_index=True) # 将每一个省份的数据合并到总数据中 Anaconda 警告了 C:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py:9: FutureWarning: Sorting because non-concatenation axis is not aligned. A future version
of pandas will change to not sort by default.
To accept the future behavior, pass 'sort=False'.
To retain the current behavior and silence the warning, pass 'sort=True'.
if __name__ == '__main__': 最后提出来才424行,
目前没找到有效的提取方法,一直在用手动输入城市名字 |
如果方便的话可以发一下完整的代码,我可以来看看。或者也可以参考我的script.py文件。 |
Hi, import pandas as pd
import os
import numpy
import requests
url = 'https://lab.isaaclin.cn/nCoV/api/area?latest=0'
r = requests.request('GET', url)
data = r.json()
#df = pd.DataFrame.from_records(data['results'])
from datetime import datetime
#data = pd.DataFrame()
temp = pd.DataFrame(r.json()['results'][0]['cities'])
# # 其他变量也可以通过此方法处理
temp['province_confirmedCount'] = r.json()['results'][0]['confirmedCount']
# data = pd.DataFrame()
from pandas import concat
for province in r.json()['results']:
temp = pd.DataFrame(province['cities'])
temp['province_confirmedCount'] = province['confirmedCount']
data = pd.concat([data, temp], ignore_index=True) # 将每一个省份的数据合并到总数据中
|
你的代码中由于 你如果需要用 |
已知用以下python代码能获取全国的数据
但当我们需要读取城市/区域的数据将
all
改成area
时输出嵌套着list,不知道如何解决?
The text was updated successfully, but these errors were encountered: