1

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?

1
  • Do you mind providing a sample of the data? Commented Aug 7, 2021 at 21:37

2 Answers 2

1

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()
Sign up to request clarification or add additional context in comments.

2 Comments

OK, and what if i must compare float values with 1
Not a problem, you can compare them if you want or convert all of them to floats using df.astype
0
sub_df = df.loc[df['avg_speed'] <1] 

plt.hist(sub_df)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.