0

Beginner coder here, thanks in advance!

I want to read the values of a csv file that are between a range. Right now, I am using pd.read_csv from pandas, and I want my output to look the same as what reading the csv file would look like. What I have so far is:

x = pd.read_csv("pathfile", parse_dates=True, index_col="timestamp")
x_lower = 3
x_higher = 8

x_ideal = x[x <= x_higher and x_lower <= x]
print(x_ideal)

But I keep getting the ValueError (builtins.ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().). I can get the ranges above x_lower using the code:

x_ideal = x[x_lower <= x]

And the ranges below x_higher using the code:

x_ideal = x[x <= x_higher]

I just can't get the range between them. For the record, I've tried something like this too, which raises the same error:

x_ideal = x[x_lower <= x <= x_higher]

An example csv would be:

date,numerical value
Monday,0
Tuesday,1
Wednesday,2
Thursday,3
Friday,4
Saturday,8
Sunday,9
Monday,10
Tuesday,11
Wednesday,12
Thursday,4
Friday,7
Serena
  • 75
  • 7

1 Answers1

0

You can use between on the column numerical value and not on whole dataframe:

x_lower = 3
x_higher = 8
x_ideal = x[x['numerical value'].between(x_lower, x_higher)]

Output:

>>> x_ideal
        date  numerical value
3   Thursday                3
4     Friday                4
5   Saturday                8
10  Thursday                4
11    Friday                7

To know more about your error, follow this link and read the accepted answer.

Corralien
  • 109,409
  • 8
  • 28
  • 52
  • Oops, thank you so much! I didn't find that question (probably because I used the wrong terms). Really appreciate your help!! – Serena Sep 17 '21 at 21:16