2017-06-24 92 views
2

我想從數據框中繪製條形圖。這裏是數據幀 enter image description here 而當我嘗試寫代碼來顯示barplot時,它會返回此錯誤。 TypeError: unsupported operand type(s) for /: 'str' and 'int' 我GOOGLE了它,很多人都添加了這個關鍵字kind="count",但dint的工作。這是我正在使用的代碼。顯示barplot的seaborn錯誤

#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() 

回答

3

您需要將列user_countint,因爲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() 

graph

+0

謝謝!!我認爲標題很重要。 – Aurora

+0

@Aurora標題無關緊要,但數據類型的確很重要 –