显示条形图的 seaborn 错误
seaborn error for showing barplot
我正在尝试从数据框中绘制条形图。这是数据框
当我尝试编写代码来显示条形图时,它 returns 这个错误。
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()
您需要将列 user_count
转换为 int
,因为 dtype
是 object
显然 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()
我正在尝试从数据框中绘制条形图。这是数据框
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()
您需要将列 user_count
转换为 int
,因为 dtype
是 object
显然 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()