3

I have an ascii file as following (a sample)

id lon lat val1 val2 val3
1 22 38 67 66 87 89 
2 23.5 39 56 10 90 98
3 22.5 38.5 34 45 56 78 

For specific points (lat,lon) I want to set to zero the variables val1, val2,val3. e.g. for lon=22, lat=38 and lon=23.5,lat=39

i tried the following (only for val1 modification) and I got ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). How I could do that (setting also to 0 all the variables val)

import pandas as pd
col_names=['id','lon','lat','val1','val2','val3']
df = pd.read_csv(i,sep='\s+',names=col_names,header=None) 
df.loc[df['Lon'] ==22 and df['Lat'] ==38, 'val1'] = 0
    
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Nat
  • 325
  • 2
  • 13

3 Answers3

1

Try adding brackets.

df.loc[(df['Lon'] ==22) & (df['Lat'] ==38), 'val1'] = 0
Tanishq
  • 58
  • 3
1

Instead of

df['Lon'] ==22 and df['Lat'] ==38

use

(df['Lon'] ==22) & (df['Lat'] ==38)
Virtuoz
  • 896
  • 1
  • 8
  • 14
  • Thank you. However , how I could set all variables to zero? e.g. val1,val2,val3? Do I have to do the same for other variables? Is there any other simple way? Since the original file has 24 variables – Nat Sep 26 '21 at 20:04
  • How I could use apply in that case? – Nat Sep 26 '21 at 20:12
1

If you have multiple columns that start with val to process at a step, you can use .filter() to filter the columns and set it into a list cols. Then, use .loc to set the selected columns, as follows:

# put all columns that start with `val` into a list
cols = df.filter(regex='^val').columns

# set 0 all the variables val*
df.loc[(df['Lon'] == 22) & (df['Lat'] == 38), cols] = 0
SeaBean
  • 22,547
  • 3
  • 13
  • 25
  • thank you, what if I would like to multiply by a factor the cols? Do I have to use df.loc[(df['Lon'] == 22) & (df['Lat'] == 38), cols] = cols * factor ? – Nat Sep 27 '21 at 10:19
  • 1
    @Nat Use `df.loc[(df['Lon'] == 22) & (df['Lat'] == 38), cols] *= factor` – SeaBean Sep 27 '21 at 10:21