1
import pandas as pd
city_temp = pd.read_csv('weather trend/Tokyo_Japan.csv')
city_temp.head()

Then I want to extract the specific range of year like between 1900-1950. so I used this code like below and I got an error saying "name 'year' is not defined"

city_temp[year >= 1900 & year <= 1950].avg_temp

plus I tried this code and got an valuerror 'The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

city_temp[city_temp.year >= 1900 & city_temp.year <= 1950]. avg_temp

How can I fix this?

momentum
  • 23
  • 5
  • 1
    Please don't post images of code and errors. See [here](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) and [edit](https://stackoverflow.com/posts/65376104/edit) your question please. – costaparas Dec 20 '20 at 01:24
  • okay thanks. I'll edit this – momentum Dec 20 '20 at 01:27

2 Answers2

1

the variable year is not defined. you need to use city_temp["year"] instead

You should also wrap each of the conditions inside parentheses like this:

city_temp[(city_temp["year"] >= 1900) & (city_temp["year"] <= 1950)]

This is because the & operator is prioritized over the >= and <= operators. (just like multiplication comes over addition in mathematics.)

sommervold
  • 301
  • 2
  • 8
1

You can also use pandas between:

city_temp[city_temp.year.between(1900, 1950)]
Cainã Max Couto-Silva
  • 4,839
  • 1
  • 11
  • 35