【问题标题】:seaborn error for showing barplot显示条形图的 seaborn 错误
【发布时间】:2017-06-25 03:34:20
【问题描述】:

我正在尝试从数据框中绘制条形图。这是数据框 当我尝试编写代码来显示条形图时,它会返回此错误。 TypeError: unsupported operand type(s) for /: 'str' and 'int' 我用谷歌搜索了它,很多人添加了这个关键字kind="count",但一点作用都没有。这是我正在使用的代码。

#Using seaborn to get the hours against the user_count

sns.set_style("whitegrid")

ax = sns.barplot(x= 'hours', y= 'user_count', data=dff)
ax.set(ylabel = 'User Count')
ax.set(xlabel = 'Hour of the Day')
ax.set_title('2017-06-02/ Friday')
plt.show()

【问题讨论】:

    标签: python pandas seaborn


    【解决方案1】:

    您需要将列user_count 转换为int,因为dtypeobject 显然是string 值:

    dff = pd.DataFrame({'hours':[0,1,2], 'user_count':['2','4','5']})
    print (dff)
       hours user_count
    0      0          2
    1      1          4
    2      2          5
    
    print (dff.dtypes)
    hours          int64
    user_count    object
    dtype: object
    
    sns.set_style("whitegrid")
    
    dff['user_count'] = dff['user_count'].astype(int)
    
    ax = sns.barplot(x= 'hours', y= 'user_count', data=dff)
    ax.set(ylabel = 'User Count')
    ax.set(xlabel = 'Hour of the Day')
    ax.set_title('2017-06-02/ Friday')
    plt.show()
    

    【讨论】:

    • 谢谢!!我认为标题无关紧要。
    • @Aurora 标题无关紧要,但数据类型很重要
    猜你喜欢
    • 1970-01-01
    • 2019-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-08
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    相关资源
    最近更新 更多