Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: unsupported operand type(s) for -: 'str' and 'float' python pandas

Tags:

python

pandas

I have a dataframe I usesd groupby to get the table below

    time_groups=time_data.groupby('time_binned').mean()
    time_groups

**enter image description here**

Then I try to build a bar chart to show the 'Status' column result, but I got error TypeError: unsupported operand type(s) for -: 'str' and 'float'

plt.bar(time_groups.index.astype(str),100*time_groups['Status'])
like image 486
qing zhangqing Avatar asked Sep 28 '26 11:09

qing zhangqing


1 Answers

The reason you see the error is because the first argument (x) is required to be numeric. This is because you're specifying the x-coordinates of the plot. Try doing this:

plt.bar(x = np.arange(0, len(time_groups['Status'])), height = time_groups['Status'])
plt.xticks(np.arange(0, len(time_groups['Status'])), time_groups.index.values)
plt.show()
like image 200
Vishnu Kunchur Avatar answered Oct 01 '26 02:10

Vishnu Kunchur