Python – Type error: unsupported operand type(s) for -: ‘str’ and ‘float’ python pandas

Type error: unsupported operand type(s) for -: ‘str’ and ‘float’ python pandas… here is a solution to the problem.

Type error: unsupported operand type(s) for -: ‘str’ and ‘float’ python pandas

I

have a data frame and I use groupby to get the following table

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

**enter image description here**

Then I tried building a bar chart to show the Status column results,
But I get the error TypeError: unsupported operand type(s) for -: ‘str’ and ‘float’

plt.bar(time_groups.index.astype(str),100*time_groups['Status'])

Solution

The reason you see the error is that the first parameter (x) must be a number. This is because you are specifying the x-coordinate of the plot. Try to do 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()

Related Problems and Solutions