CSV 文件的数据可视化与破折号

Data visualization of CSV file with dash

我是 Python 的新手。 https://realpython.com/python-dash 提供使用 Python 的破折号从 CSV 文件可视化折线图的代码。

我运行下面的代码,但是收到一个错误。

import dash_core_components as dcc
import dash_html_components as html
import pandas as pd

data = pd.read_csv("avocado.csv")
data = data.query("type == 'conventional' and region == 'Albany'")
data["Date"] = pd.to_datetime(data["Date"], format="%Y-%m-%d")
data.sort_values("Date", inplace=True)

app = dash.Dash(__name__)

app.layout = html.Div(
    children=[
        html.H1(children="Avocado Analytics",),
        html.P(
            children="Analyze the behavior of avocado prices"
            " and the number of avocados sold in the US"
            " between 2015 and 2018",
        ),
        dcc.Graph(
            figure={
                "data": [
                    {
                        "x": data["Date"],
                        "y": data["AveragePrice"],
                        "type": "lines",
                    },
                ],
                "layout": {"title": "Average Price of Avocados"},
            },
        ),
        dcc.Graph(
            figure={
                "data": [
                    {
                        "x": data["Date"],
                        "y": data["Total Volume"],
                        "type": "lines",
                    },
                ],
                "layout": {"title": "Avocados Sold"},
            },
        ),
    ]
)

if __name__ == "__main__":
    app.run_server(debug=True)
Traceback (most recent call last):
  File "/Users/halcyon/Documents/Python/Dashboard - Avocado prices/app.py", line 8, in <module>
    data["Date"] == pd.to_datetime(data["Date"], format="%Y-%m-%d")
  File "/Users/halcyon/Documents/Python/Dashboard - Avocado prices/venv/lib/python3.9/site-packages/pandas/core/ops/common.py", line 64, in new_method
    return method(self, other)
  File "/Users/halcyon/Documents/Python/Dashboard - Avocado prices/venv/lib/python3.9/site-packages/pandas/core/ops/__init__.py", line 529, in wrapper
    res_values = comparison_op(lvalues, rvalues, op)
  File "/Users/halcyon/Documents/Python/Dashboard - Avocado prices/venv/lib/python3.9/site-packages/pandas/core/ops/array_ops.py", line 247, in comparison_op
    res_values = comp_method_OBJECT_ARRAY(op, lvalues, rvalues)
  File "/Users/halcyon/Documents/Python/Dashboard - Avocado prices/venv/lib/python3.9/site-packages/pandas/core/ops/array_ops.py", line 57, in comp_method_OBJECT_ARRAY
    result = libops.scalar_compare(x.ravel(), y, op)
  File "pandas/_libs/ops.pyx", line 84, in pandas._libs.ops.scalar_compare
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

我复制并粘贴了教程中显示的代码,但无法重现。我试图 Google 并从回溯日志中理解 material 但无法理解它。

  • 我没有看到它已在评论中修复。几个小的变化,使其可重现
    1. github 动态获取数据而不是希望它在文件系统上
    2. 使用 JupyterDash 开箱即用 plotly 5.x.y
import dash_core_components as dcc
import dash_html_components as html
from jupyter_dash import JupyterDash
import pandas as pd
import requests
import io

# data = pd.read_csv("avocado.csv")
data = pd.read_csv(io.StringIO(requests.get("https://raw.githubusercontent.com/chainhaus/pythoncourse/master/avocado.csv").text))
data = data.query("type == 'conventional' and region == 'Albany'")
data["Date"] = pd.to_datetime(data["Date"], format="%Y-%m-%d")
data.sort_values("Date", inplace=True)

app = JupyterDash(__name__)
# app = dash.Dash(__name__)