gpt4 book ai didi

python - 想要按大陆与 Pandas 对 Covid 19 数据集进行分组,但假设的可视化在 Plotly 中是错误的

转载 作者:行者123 更新时间:2023-12-04 09:29:33 24 4
gpt4 key购买 nike

我对这一切都很陌生,现在我正在学习 Python。我知道基础知识,但现在我有一个 covid-19 数据集的问题,我想按大陆分组以获得欧洲、亚洲等的总死亡率。当我查看可视化时,我只看到“其他”和太多的线条。希望你能帮助我并告诉我我做错了什么。现在我认为问题在于 for 循环。
我的代码:

import pandas as pd
import plotly.offline as pyo
import plotly.graph_objs as go

df = pd.read_csv("https://opendata.ecdc.europa.eu/covid19/casedistribution/csv")
df.set_index("continentExp", inplace=True)
del df["countryterritoryCode"], df["Cumulative_number_for_14_days_of_COVID-19_cases_per_100000"], df["geoId"], df["countriesAndTerritories"]

data = []

for name in df.index.unique():
trace = go.Scatter(
x = df["dateRep"],
y = df["deaths"],
name = name,
mode = "lines"
)
data.append(trace)

layout = go.Layout(
title = "Covid-19 Dashboard",
xaxis = {"title" : "Datum"},
yaxis = {"title" : "Tote"})

fig = go.Figure(data=data, layout=layout)

pyo.plot(fig)

最佳答案

在我看来你有问题 pandas而不是与 plotly .如果您正在按大陆寻找每日死亡人数,您应该使用 groupby。
整理资料

import pandas as pd
import plotly.express as px
import plotly.graph_objs as go

df = pd.read_csv("https://opendata.ecdc.europa.eu/covid19/casedistribution/csv")

# better to have data as datetime
df["dateRep"] = df["dateRep"].astype("M8")
# the previous is equivalent to
# df["dateRep"] = pd.to_datetime(df["dateRep"])

# now you want to look for the daily deaths
# for every continent
grp = df.groupby(["continentExp","dateRep"])["deaths"]\
.sum().reset_index()
使用 plotly.express
fig = px.line(grp, 
x="dateRep",
y="deaths",
color="continentExp",
labels={"deaths":"Tote",
"dateRep":"Datum"})

fig.update_layout(title="Covid-19 Dashboard",
title_x=0.5)

enter image description here
使用 plotly.graph_objs
fig = go.Figure()
continents = grp["continentExp"].unique()
for continent in continents:
ts = grp[grp["continentExp"]==continent]
fig.add_trace(
go.Scatter(x=ts["dateRep"],
y=ts["deaths"],
name=continent))

fig.update_layout(title="Covid-19 Dashboard",
title_x=0.5,
xaxis={"title" : "Datum"},
yaxis={"title" : "Tote"})
enter image description here

关于python - 想要按大陆与 Pandas 对 Covid 19 数据集进行分组,但假设的可视化在 Plotly 中是错误的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62901187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com