Given a DataFrame df
, if one wanted to filter the DataFrame to rows that have column_4
between 45 and 48, one might try the following code snippet:
import pandas as pd
df = pd.DataFrame({f'column_{i}': range(i*10, 10*i+10) for i in range(10)})
df2 = df[45 < df['column_4'] < 48]
However, this code yields a ValueError, because The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
One instead must do
import pandas as pd
df = pd.DataFrame({f'column_{i}': range(i*10, 10*i+10) for i in range(10)})
df2 = df[(45 < df['column_4']) & (df['column_4'] < 48)]
Why is this the case - and is this a hacky way to filter a DataFrame?
This question has a seemingly partial answer here, however that does not seem to address specifically why 45 < a < 48
does not work.