DataFrame
object has powerful and flexible replace
method:
DataFrame.replace(
to_replace=None,
value=None,
inplace=False,
limit=None,
regex=False,
method='pad',
axis=None)
Note, if you need to make changes in place, use inplace
boolean argument for replace
method:
inplace: boolean, default
False
IfTrue
, in place. Note: this will modify any other views on this object (e.g. a column form a DataFrame). Returns the caller if this isTrue
.
df['BrandName'].replace(
to_replace=['ABC', 'AB'],
value='A',
inplace=True
)
You could also pass a dict
to the pandas.replace
method:
data.replace({
'column_name': {
'value_to_replace': 'replace_value_with_this'
}
})
This has the advantage that you can replace multiple values in multiple columns at once, like so:
data.replace({
'column_name': {
'value_to_replace': 'replace_value_with_this',
'foo': 'bar',
'spam': 'eggs'
},
'other_column_name': {
'other_value_to_replace': 'other_replace_value_with_this'
},
...
})
Just wanted to show that there is no performance difference between the 2 main ways of doing it:
df = pd.DataFrame(np.random.randint(0,10,size=(100, 4)), columns=list('ABCD'))
def loc():
df1.loc[df1["A"] == 2] = 5
%timeit loc
19.9 ns ± 0.0873 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
def replace():
df2['A'].replace(
to_replace=2,
value=5,
inplace=True
)
%timeit replace
19.6 ns ± 0.509 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
loc function can be used to replace multiple values, Documentation for it : loc
df.loc[df['BrandName'].isin(['ABC', 'AB'])]='A'
Created the Data frame:
import pandas as pd
dk=pd.DataFrame({"BrandName":['A','B','ABC','D','AB'],"Specialty":['H','I','J','K','L']})
Now use DataFrame.replace()
function:
dk.BrandName.replace(to_replace=['ABC','AB'],value='A')
This solution will change the existing dataframe itself:
mydf = pd.DataFrame({"BrandName":["A", "B", "ABC", "D", "AB"], "Speciality":["H", "I", "J", "K", "L"]})
mydf["BrandName"].replace(["ABC", "AB"], "A", inplace=True)
Source: Stackoverflow.com