How can I replace multiple values in one column using Pandas?

How can I replace multiple values in one column using Pandas?

Pandas is a popular library in Python used for data analysis and manipulation. It provides various functions to efficiently modify and update data in a DataFrame. One common task is to replace multiple values in a single column of a DataFrame. This can be achieved by using the “replace” function in Pandas, which allows for replacing specified values with desired values. By specifying the column name and a dictionary mapping the old values to new values, multiple replacements can be performed in one go. This offers a convenient and efficient way to clean and update data in a DataFrame.

Pandas: Replace Multiple Values in One Column


You can use the following basic syntax to replace multiple values in one column of a pandas DataFrame:

df = df.replace({'my_column' : {'old1' : 'new1', 'old2' : 'new2', 'old3' : 'new3'}})

The following example shows how to use this syntax in practice.

Example: Replace Multiple Values in One Column in Pandas

Suppose we have the following pandas DataFrame that contains information about various basketball players:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'position': ['G', 'G', 'F', 'F', 'F', 'C', 'C'],
                   'points': [28, 17, 19, 14, 23, 26, 5],
                   'rebounds': [5, 6, 4, 7, 14, 12, 9],
                   'assists': [10, 13, 7, 8, 4, 5, 8]})

#view DataFrame
print(df)

  position  points  rebounds  assists
0        G      28         5       10
1        G      17         6       13
2        F      19         4        7
3        F      14         7        8
4        F      23        14        4
5        C      26        12        5
6        C       5         9        8

Suppose we would like to make the following replacements in the position column:

  • Replace ‘G’ with ‘Guard’
  • Replace ‘F’ with ‘Forward’
  • Replace C with ‘Center’

We can use the following syntax to do so:

#replace multiple values in position column
df = df.replace({'position' : {'G' : 'Guard', 'F' : 'Forward', 'C' : 'Center'}})

#view updated DataFrame
print(df)

  position  points  rebounds  assists
0    Guard      28         5       10
1    Guard      17         6       13
2  Forward      19         4        7
3  Forward      14         7        8
4  Forward      23        14        4
5   Center      26        12        5
6   Center       5         9        8

Notice that multiple values have been replaced in the position column.

We can use similar syntax to replace multiple values in a numeric column.

For example, the following code shows how to make the following replacements in the assists column:

  • Replace 10 with 20
  • Replace 13 with 15
  • Replace 8 with 10

We can use the following syntax to do so:

#replace multiple values in assists column
df = df.replace({'assists' : {10:20, 13:15, 8:10}})

#view updated DataFrame
print(df)

  position  points  rebounds  assists
0        G      28         5       20
1        G      17         6       15
2        F      19         4        7
3        F      14         7       10
4        F      23        14        4
5        C      26        12        5
6        C       5         9       10

Notice that multiple values have been replaced in the assists column.

The following tutorials explain how to perform other common tasks in pandas:

How to Replace NaN Values with Zeros in Pandas

Cite this article

stats writer (2024). How can I replace multiple values in one column using Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-replace-multiple-values-in-one-column-using-pandas/

stats writer. "How can I replace multiple values in one column using Pandas?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-replace-multiple-values-in-one-column-using-pandas/.

stats writer. "How can I replace multiple values in one column using Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-replace-multiple-values-in-one-column-using-pandas/.

stats writer (2024) 'How can I replace multiple values in one column using Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-replace-multiple-values-in-one-column-using-pandas/.

[1] stats writer, "How can I replace multiple values in one column using Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I replace multiple values in one column using Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top