![]() |
VOOZH | about |
While working with data in Pandas, we often need to modify or transform values in specific columns. One common transformation is remapping values using a dictionary. This technique is useful when we need to replace categorical values with labels, abbreviations or numerical representations. In this article, we'll explore different ways to remap values in a Pandas DataFrame using dictionary mapping.
Let's start by creating a sample DataFrame that contains event details.
Date Event Cost 0 10/2/2011 Music 10000 1 11/2/2011 Poetry 5000 2 12/2/2011 Theatre 15000 3 13/2/2011 Comedy 2000
The replace() function in Pandas allows us to remap values using a dictionary. It works directly on a DataFrame column and modifies the values based on the provided mapping.
Date Event Cost 0 10/2/2011 M 10000 1 11/2/2011 P 5000 2 12/2/2011 T 15000 3 13/2/2011 C 2000
Explanation:
Another way to remap values in a Pandas column is by using the map() function.
Date Event Cost 0 10/2/2011 M 10000 1 11/2/2011 P 5000 2 12/2/2011 T 15000 3 13/2/2011 C 2000
Explanation:
Feature | replace() | map() |
|---|---|---|
Works on entire DataFrame? | Yes | No, only on Series (columns) |
Supports multiple column replacements? | Yes | No, works on a single column |
Handles missing keys? | Yes | No, returns NaN for missing keys |
If the dictionary used in map() does not contain a key for some values in the column, those values will be replaced with NaN. To handle this, we can use the fillna() function.
Date Event Cost 0 10/2/2011 M 10000 1 11/2/2011 P 5000 2 12/2/2011 Unknown 15000 3 13/2/2011 Unknown 2000
Explanation: