Home > Net >  Applying an IF condition in multiple columns with pandas
Applying an IF condition in multiple columns with pandas

Time:09-27

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
    

CodePudding user response:

Try adding brackets.

df.loc[(df['Lon'] ==22) & (df['Lat'] ==38), 'val1'] = 0

CodePudding user response:

Instead of

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

use

(df['Lon'] ==22) & (df['Lat'] ==38)

CodePudding user response:

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
  • Related