![](/img/trans.png)
[英]Why won't my Plotly express figure work? - “ValueError: Plotly Express cannot process wide-form data with columns of different type.”
[英]How do I plot this line graph with the date on the x axis ? Error: "Plotly Express cannot process wide-form data with columns of different type."
我的数据框如下所示:
标题 | 注释 | 日期 |
---|---|---|
帖子1 | 256 | 2021-07-19 11:48:39 |
后2 | 454 | 2021-07-18 22:14:41 |
后 3 | 452 | 2019-05-14 19:38:11 |
帖子 4 | 422 | 2018-06-14 16:38:12 |
后5 | 452 | 2017-03-04 17:18:11 |
我想制作一个折线图,x 轴显示年份,y 轴显示当年发布的帖子数量(2021 年 2 个,2019 年 1 个等)
titles_values = df["title"].value_counts().sum()
fig = px.line(data_frame=df, x="time" , y=titles_values)
fig.show()
我得到的错误是:“Plotly Express 无法处理具有不同类型列的宽格式数据。”
我不知道如何让它工作。
这是总结数据框的一个简单案例。 df["date"].dt.year, as_index=False).size()
计算每年的行数
import io
import plotly.express as px
import pandas as pd
df = pd.read_csv(io.StringIO("""title,comments,date
post1,256,2021-07-19 11:48:39
post2,454,2021-07-18 22:14:41
post3,452,2019-05-14 19:38:11
post4,422,2018-06-14 16:38:12
post5,452,2017-03-04 17:18:11"""))
df["date"] = pd.to_datetime(df["date"])
px.line(
df.groupby(df["date"].dt.year, as_index=False).size(), x="date", y="size"
).update_layout(xaxis={"type": "category"}, yaxis={"dtick":1,"rangemode":"tozero"})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.