Search code examples
pythonpandas

TypeError: unsupported operand type(s) for -: 'str' and 'float' 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'])

Solution

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