express plotly - TypeError: unsupported operand type(s) for : 'str' and 'int' 的操作数类型不被支持

问题描述 投票:0回答:1

我想用百分比列抛出express plotly来绘制数据框。

接收

 TypeError: unsupported operand type(s) for /: 'str' and 'int'

栏目。

    Meeting_inv pchanges_dax    Unscheduled_orde    pharmacy_nam    dis  yyyy/mm
0   1%             17%                5                  x           r  2011-1
1   2%             11%                1                  x           r  2011-2
2   5%             10%                7                  x           r  2011-6
3   10%            10%                5                  x           r  2011-7
4   2%             5%                14                  x           r  2011-8  

代码:

    import plotly.express as px

px.scatter(df3, x="Meeting_inventory_target", y="Unscheduled_orders", animation_frame="yyyy/mm", animation_group="pharmacy_name",
           size="pchanges_dax", color="pharmacy_name", hover_name="district",
           log_x=True, size_max=100, range_x=[0.1,0.600], range_y=[0,30])
尝试转换列 - 没有帮助。

tnx in advance

python express plot plotly percentage
1个回答
0
投票

问题出在Meeting_inv和pchanges_dax列中的百分号上,pandas(和plotly)将它们视为字符串,而不是整数。

要解决这个问题,请运行。

df["Meeting_inv"] = df.Meeting_inv.str.replace("%", "").astype(int)
df["pchanges_dax"] = df.pchanges_dax.str.replace("%", "").astype(int)

然后创建绘图

© www.soinside.com 2019 - 2024. All rights reserved.