pythonpandasmatplotlibmpld3

Python: float() argument must be a string or a number,not 'pandas


Have the following piece of code through which I am trying to plot a graph:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import mpld3



my_list = [1,2,3,4,5,7,8,9,11,23,56,78,3,3,5,7,9,12]

new_list = pd.Series(my_list)

df1 = pd.DataFrame({'Range1':new_list.value_counts().index, 'Range2':new_list.value_counts().values})

df1.sort_values(by=["Range1"],inplace=True)

df2 = df1.groupby(pd.cut(df1["Range1"], [0,1,2,3,4,5,6,7,8,9,10,11,df1['Range1'].max()])).sum()

objects = df2['Range2'].index

y_pos = np.arange(len(df2['Range2'].index))

plt.bar(df2['Range2'].index.values, df2['Range2'].values)

but getting the following error message:

TypeError: float() argument must be a string or a number, not 'pandas._libs.interval.Interval'

Not getting from where this float error is coming. Any suggestion is highly appreciated.


Solution

  • Matplotlib cannot plot category datatypes. You would need to convert to a string.

    plt.bar(df2['Range2'].index.astype(str), df2['Range2'].values)
    

    enter image description here