Search code examples
pythonpandasmatplotlibhistogram

Create a histogram, ignoring some certain values


Have to create a histogram, neglecting the values higher than constant

I try this:

plt.hist(df[df['avg_speed'] < 1])
plt.show()

but it's not working, because of comparison returns the boolean values. How to repair it?


Solution

  • I suggest you check the types of your dataframe columns printing df.dtypes is one way of doing it.

    Make sure they integers (int64).

    I tried your code and it works with a simple table.

        import pandas as pd
    
        data = {"avg_speed": [1,2,3,4,5,6]}
        df = pd.DataFrame(data)
        table = df[df["avg_speed"] > 1]
        plt.hist(table.avg_speed)
        plt.show()