带有破折号的 CSV 文件的数据可视化
Posted
技术标签:
【中文标题】带有破折号的 CSV 文件的数据可视化【英文标题】:Data visualization of CSV file with dash 【发布时间】:2021-10-19 20:13:47 【问题描述】:我是 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 搜索并了解追溯日志中的材料,但无法理解。
【问题讨论】:
您好,您能确认您发布的代码是引发错误的版本吗?询问,因为错误消息指的是data["Date"] == pd.to_datetime(data["Date"]
而您的代码有 data["Date"] = pd.to_datetime(data["Date"]
。这需要是=
(分配),而不是==
(比较)。
@patrick 成功了!太感谢了!我手动输入了该部分,然后复制并粘贴了其余部分 - 我真的很感激。
【参考方案1】:
我没有看到它已在 cmets 中修复。几个小改动使其可重现
-
从 github 动态获取数据,而不是希望它在文件系统上
使用 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__)
【讨论】:
谢谢罗伯!非常感谢您宝贵的时间。以上是关于带有破折号的 CSV 文件的数据可视化的主要内容,如果未能解决你的问题,请参考以下文章