想将一个新冠病毒19的数据集按大陆与大Pandas分组,但假设的可视化在情节上是错误的

2024-03-29 13:23:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我对这一切都很陌生,现在我正在学习Python。我知道基本情况,但现在我对新冠病毒-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)

Tags: csvnameimportgodffordataindex
1条回答
网友
1楼 · 发布于 2024-03-29 13:23:32

在我看来,你对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

相关问题 更多 >