Change language

Python | Pandas dataframe.replace ()

| |

Pandas function dataframe.replace() is used to replace list , Python .

Syntax: DataFrame.replace (to_replace = None, value = None, inplace = False, limit = None, regex = False, method = ’pad’, axis = None)

Parameters:
to_replace: [str, regex, list, dict, Series, numeric, or None] pattern that we are trying to replace in dataframe.
value: Value to use to fill holes (eg 0), alternately a dict of values ​​specifying which value to use for each column (columns not in the dict will not be filled). Regular expressions, strings and lists or dicts of such objects are also allowed.
inplace: If True, in place. Note: this will modify any other views on this object (eg a column from a DataFrame). Returns the caller if this is True.
limit: Maximum size gap to forward or backward fill
regex: Whether to interpret to_replace and / or value as regular expressions. If this is True then to_replace must be a string. Otherwise, to_replace must be None because this parameter will be interpreted as a regular expression or a list, dict, or array of regular expressions.
method: Method to use when for replacement, when to_replace is a list.

Returns: filled: NDFrame

To link to the CSV file used in the code, press here

Example # 1: replace the Boston Celtics command to the Omega Warrior in nba.csv

# import pandas as pd

import pandas as pd

 
# Create data frame from CSV file

df = pd.read_csv ( "nba.csv" )

 
# Print the first 10 lines of the data frame for rendering

df [: 10 ]

Exit:

We’re going to replace the Boston Celtics command with Omega Warrior in the df data frame

# this will replace the "Boston Celtics" to & quot; Omega Warrior & quot;

df.replace (to_replace = "Boston Celtics" ,

value = "Omega Warrior" )

Exit:

Example # 2: Replacing more than one value at a time ... Using Python List as Argument

We’re going to replace Boston Celtics and Texas with Omega Warrior in the df data frame.

# import pandas as pd

import pandas as pd

 
# Create a data frame from a CSV file

df = pd.read_csv ( "nba.csv" )

 
# this will replace the Boston Celtics and & quot; Texas & quot; to & quot; Omega Warrior & quot;

df.replace (to_replace = [ "Boston Celtics" , "Texas" ], 

value = "Omega Warrior" )

Output:

Notice the College column in the first row: Texas has been replaced by Omega Warriors.

Example # 3: replace the Nan value in the data frame with -99999.

# import pandas as pd

import pandas as pd

 
# Create a data frame from a CSV file

df = pd.read_csv ( "nba.csv" )

 
# replaces the Nan value in the dataframe with -99999

df .replace (to_replace = np.nan, value = - 99999 )

Output:

Note that all Nan values ​​in the data frame have been replaced with -99999. Although for practical purposes, we have to be careful what value we replace with nan .

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically