Jamie
Jamie

Reputation: 157

Pandas change one columns data if another columns data is in a list

I have some data in a data frame like this:

COLUMNS Point,x,y,z,Description,Layer,Date
1,224939.203,1243008.651,1326.774,F,C-GRAD-FILL,09/22/18 07:24:34,
1,225994.242,1243021.426,1301.772,BS,C-GRAD-FILL,09/24/18 08:24:18,
451,225530.332,1243016.186,1316.173,GRD,C-TOE,10/02/18 11:49:13,
452,225522.429,1242996.017,1319.168,GRD,C-TOE KEY,10/02/18 11:49:46,

I would like to try to try to check against a list of strings to see if it matches, and then change another columns value.

myList = ["BS", "C"]
if df['Description'].isin(myList)) == True:
     df['Layer']="INLIST"
     df['Date']="1/1/2018'

Upvotes: 4

Views: 89

Answers (2)

BENY
BENY

Reputation: 323326

Borrow the mask from jpp and using list to assign the value

df.loc[mask,['Layer','Date']]=['INLIST','1/1/2018']

Upvotes: 1

jpp
jpp

Reputation: 164773

Use a mask with pd.DataFrame.loc:

mask = df['Description'].isin(['BS', 'C'])
df.loc[mask, 'Layer'] = 'INLIST'
df.loc[mask, 'Date'] = '1/1/2018'

Alternatively, use pd.Series.mask:

mask = df['Description'].isin(['BS', 'C'])
df['Layer'].mask(mask, 'INLIST', inplace=True)
df['Date'].mask(mask, '1/1/2018', inplace=True)

Note you should probably store dates as datetime objects, e.g. pd.Timestamp('2018-01-01'), or convert your series via pd.to_datetime. There's usually no need for a Python-level loop.

Upvotes: 4

Related Questions