Pandas replace values in column

In this article, we will discuss how to replace values in column of pandas dataframe.

Using the loc() function to replace values in column of pandas DataFrame

The loc() function is used to access values based on column names and row values. We can use this function to access the required value and provide the new value using the = operator.

For example,

Output:

In the above example, we changed Jay to Jack in column a.

We can use this function to replace values based on some conditions also.

See the following example.

Output:

In the above example, we found all the values below 75 in column b and replaced them with 60.

Using the iloc() function to to replace values in column of pandas DataFrame

The iloc() function is similar to the loc() function and can be used to access columns and rows of a DataFrame. It uses the index of the values instead of their labels.

For example,

Output:

Using the map() function to replace values of a column in a pandas DataFrame

The map() function can apply some function or collector on all the elements of a Series object or a DataFrame. We can use it to detect and replace values of a column in a DataFrame. We have to specify the old and new values within the function.

For example,

Output:

Note that we also need to name the element we do not wish to replace otherwise, they will get replaced with the NaN constant.

Using the replace() function to replace values in column of pandas DataFrame

This probably the most straightforward method to replace the values of a column. We can use the replace() function to replace one or more values in a DataFrame.

In the following example, we will replace multiple values with one value.

Output:

Similarly, we can replace multiple values with different values for each.

For example,

Output:

This function can also be applied to the whole DataFrame.

Using the where() function to replace values in column of pandas DataFrame

The where() function checks the DataFrame to detect some values based on a given condition. We can replace the values which satisfy the given condition with some new value.

See the following example.

Output:

The above example replaces all values less than 80 with 60.

Using the numpy.where() function to to replace values in column of pandas DataFrame

The where() function from the numpy module is generally used with arrays only. However, since we need to change the values of a column, we can use this function with a pandas DataFrame also.

This method works similarly to the method discussed previously.

For example,

Output:

That’s all about how to replace values in column of pandas DataFrame.

Was this post helpful?

Related Posts

  • Pandas apply function to column
    12 January

    Pandas apply function to column

    Table of ContentsHow do I apply function to column in pandas?Using dataframe.apply() functionUsing lambda function along with the apply() functionUsing dataframe.transform() functionUsing map() functionUsing NumPy.square() functionWas this post helpful? We make use of the Pandas dataframe to store data in an organized and tabular manner. Sometimes there, is a need to apply a function over […]

  • Find rows with nan in Pandas
    11 January

    Find rows with nan in Pandas

    Table of ContentsWhat is nan values in Pandas?Find rows with NAN in pandasFind columns with nan in pandasFind rows with nan in Pandas using isna and iloc()Was this post helpful? In this post, we will see how to find rows with nan in Pandas. What is nan values in Pandas? A pandas DataFrame can contain […]

  • Select rows by multiple conditions using loc in Pandas
    29 July

    Pandas Loc Multiple Conditions

    💡 Outline Here is the code to select rows by pandas Loc multiple conditions. [crayon-62b82db931656433727453/] Here, we are select rows of DataFrame where age is greater than 18 and name is equal to Jay. [crayon-62b82db93165b353433580/] The loc() function in a pandas module is used to access values from a DataFrame based on some labels. It […]

  • Split dataframe in Pandas
    28 July

    Split dataframe in Pandas

    Table of ContentsUsing the iloc() function to split DataFrame in PythonBy RowsBy ColumnsUsing the sample() function to split DataFrame in PythonUsing the groupby() function to split DataFrame in PythonUsing the columns to split DataFrame in PythonWas this post helpful? In real-life scenarios, we deal with massive datasets with many rows and columns. At times, we […]

  • Read text file in Pandas
    28 July

    Read text file in Pandas

    Table of ContentsUsing the read_csv() function to read text files in PandasUsing the read_table() function to read text files in PandasUsing the read_fwf() function to read text files in PandasWas this post helpful? A dataset has the data neatly arranged in rows and columns. The pandas module in Python allows us to load DataFrames from […]

  • Copy DataFrame in Python
    10 July

    Copy DataFrame in Pandas

    This articles provide different ways to copy DataFrame in Pandas.

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.