Pandas data frame replace values in column based on condition

Question:

I have a column in data frame like this (an simplify example):

col
a
b
c
d
e
f
g

and I want to change the values like this:

col
a
b
other
other
other
other
other

I tried like this:

df = df.loc[df.col == ("a" or "b"), "col"] = "other"

but it does not work, I have an error:

AttributeError: 'str' object has no attribute 'loc'

Any advices?

Asked By: pd13

||

Answers:

The problem with your code is, that df is changing to type string during the process.

There exists a pandas function for this usecase, named pd.where().

df = df.where(df['col'].isin(['a', 'b']),'other')

Similar result avoiding where():

df[~df['col'].isin(['a', 'b'])] = 'other'
Answered By: mosc9575
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.