1
Country/Region  Lat      Long
Afghanistan     33.00    65.00
Andorra         42.5063  1.5218
Albania         41.1522  20.1633

This is my dataset I want to create a new column with the following condition if Lat < 37 and > -37 return 0

df.loc[df.Lat < 37 and df.Lat > -37, 'newcol'] = 0

This is the code I'm trying to use for the condition but I am receiving a Value error

Yigan32
  • 11
  • 4
  • Replace `df.Lat < 37 and df.Lat > -37` with `df.Lat.between(-37,37, inclusive=False)` or `df.Lat.abs() < 37`. – Quang Hoang Apr 16 '20 at 20:22
  • change and to bitwise `&` and wrap each condition in parenthsis - `df.loc[(df.Lat < 37 )& (df.Lat > -37), 'newcol'] = 0` – Umar.H Apr 16 '20 at 20:23
  • In general, don't use `and`, do use `&` and wrap the series: `(df.Lat < 37) & (df.Lat > - 37)`. – Quang Hoang Apr 16 '20 at 20:23
  • or use `np.where` - `df['new_col'] = np.where((df.Lat < 37 )& (df.Lat > -37),0,np.nan)` – Umar.H Apr 16 '20 at 20:25

1 Answers1

0

Try using the following:

df.loc[(df.Lat < 37) & (df.Lat > - 37), 'newcol'] = 0

I think the & and the | act as AND and OR in pandas.

GeneticsGuy
  • 120
  • 1
  • 1
  • 7