Pandas DataFrame | replace method
Start your free 7-days trial now!
Pandas' DataFrame.replace(~)
method replaces the specified values with another set of values.
Parameters
1. to_replace
link | string
or regex
or list
or dict
or Series
or number
or None
The values that will be replaced.
2. value
link | number
or dict
or list
or string
or regex
or None
| optional
The value(s) that will replace to_replace
. By default, value=None
.
3. inplace
link | boolean
| optional
If
True
, then the method will directly modify the source DataFrame instead of creating a new DataFrame.If
False
, then a new DataFrame will be created and returned.
By default, inplace=False
.
4. limit
link | int
| optional
The maximum number of consecutive fills to perform. By default, limit=None
.
5. regex
link | boolean
or string
| optional
If True
, then to_replace
is interpreted as a regular expression. Note that this requires to_replace
to be a string.
By default, regex=False
.
6. method
link | string
or None
| optional
The rule by which to replace to_replace
:
Method | Description |
---|---|
| Fills the value with the preceding row's value. |
| Fills the value with the next row's value. |
This parameter takes effect only when value=None
. By default, method="pad"
.
Return Value
A DataFrame
with the specified values replaced with your desired values.
Examples
Replacing single value with a single value
Consider the following DataFrame:
df
A B0 1 31 2 4
To replace all values of 1
with 5
:
A B0 5 31 2 4
Replacing multiple values with a single value
Consider the following DataFrame:
df
A B0 1 31 2 4
To replace all values of 1
and 2
with 5
:
A B0 5 31 5 4
Replacing multiple values with corresponding values
Using array
Consider the following DataFrame:
df
A B0 1 31 2 4
To replace all values of 1
and 2
with 5
and 6
, respectively:
A B0 5 31 6 4
Using dict
Consider the following DataFrame:
df
A B0 1 31 2 4
To replace all values of 1
and 3
with 5
and 6
, respectively:
A B0 5 61 2 4
Replacing using regex
Consider the following DataFrame:
df
A B0 alex cathy1 bob doge
To replace all values starting with the letter "a"
with "eric"
:
A B0 eric cathy1 bob doge
Notice how we had to enable regex by specifying regex=True
.
Replacing for certain columns only
Replacing single value with a single value
Consider the following DataFrame:
df
A B0 1 11 2 2
To replace all values of 1
with 3
for just column A
. To do so, we must provide a dict
, like follows:
A B0 3 11 2 2
Notice how column B
is unaffected despite containing a value of 1
.
Replacing multiple values with corresponding values
Consider the following DataFrame:
df
A B0 1 31 2 4
To replace values 1
and 2
with 5
and 6
respectively for just column A
:
A B0 5 31 6 4
Replacing using fills
When you don't explicitly provide the value
parameter, the replace(~)
function will automatically forward-fill the matched values, that is, replace the to_replace
with the preceding row's value.
Consider the following DataFrame:
df
A0 a1 b2 c
Forward-fill
To forward-fill all occurrences of "b"
:
A0 a1 a2 c
Notice how the value a
, which is the value preceding the match (i.e. "b"
), was used as the filler.
When there is no preceding row, nothing will be replaced.
Consider the case when we want to forward-fill all occurrences of "a"
:
A0 a1 b2 c
Notice how, even though we have "a"
in our DataFrame, it did not get replaced. Since there is no preceding row, we have no filler and hence no replacement is performed.
Backward-fill
To backward-fill all occurrences of "b"
:
A0 a1 c2 c
Notice how the value "c"
, which comes right after the match (i.e. "b"
), was used as the filler.
When there is no next row, nothing will be replaced.
Consider the case when we want to backward-fill all occurrences of "c"
:
A0 a1 b2 c
Notice how, even though we have "c"
in our DataFrame, it did not get replaced. Since there is no next row, we have no filler and hence no replacement is performed.
Limit
Consider the following DataFrame:
df
A0 a1 b2 b
By default, limit=None
, which means that there is no restriction on how many consecutive fills are allowed:
A0 a1 a2 a
In contrast, setting limit=1
yields:
A0 a1 a2 b
Here, notice how b
was filled only once. Also note that limit imposes a restriction on consecutive fills only.
Replacing in-place
To perform replacement in-place, we need to set inplace=True
. This will directly perform the replace operation on the source DataFrame instead of creating a new one.
Consider the following DataFrame:
df
A B0 1 31 2 4
We replace all occurrences of 1
with 5
with inplace=True
:
df
A B0 5 31 2 4
As shown in the output, the source DataFrame has been directly modified.