Monday, 29 January 2024

Pandas: replace string values in a DataFrame

Using 'replace()' method, we can perform string replacement operations in a dataframe

 

Example 1: Replace the values in a specific column.

df['City'] = df['City'].str.replace('Bangalore', 'Mumbai')

 

In the above example, the value 'Bangalore' in the 'City' column is replaced with 'Mumbai'. This is done by selecting the 'City' column using df['City'] and using the replace() method with the specified values to be replaced.

 

Example 2: Replace the values across dataframe by specifying a dictionary.

 

replace_dict = {'Krishna': 'Hari', 'Hyderabad': 'Delhi'}
df = df.replace(replace_dict)

 

replace_dict specifies the mapping values 'Krishna' to 'Hari' and 'Hyderabad' to 'Delhi'. The replace() method is then used on the entire DataFrame, replacing the values based on the dictionary mapping.

 

Find the below working application.

 

replace.py

 

import pandas as pd

# Create a sample DataFrame
data = {'Title': ['Krishna', 'Sailu', 'Joel', 'Chamu', 'Jitendra', "Krishna"],
        'Age': [34, 35, 234, 35, 52, 34],
        'City': ['Bangalore', 'Hyderabad', 'Hyderabad', 'Chennai', 'Bangalore', 'Chennai'],
        'Gender': ['Male', 'Female', 'Male', 'Female', 'Male', 'Male'],
        'Rating': [67, 43, 67, 100, 41, 89]}
df = pd.DataFrame(data)
print('Original DataFrame')
print(df)

df['City'] = df['City'].str.replace('Bangalore', 'Mumbai')
print('\nDataFrame after replacing City value Hyderabad to Mumbai\n', df)

# Replace values in multiple columns
replace_dict = {'Krishna': 'Hari', 'Hyderabad': 'Delhi'}
df = df.replace(replace_dict)
print('\nDataframe after replacing the dictionary of values\n', df)

Output

Original DataFrame
      Title  Age       City  Gender  Rating
0   Krishna   34  Bangalore    Male      67
1     Sailu   35  Hyderabad  Female      43
2      Joel  234  Hyderabad    Male      67
3     Chamu   35    Chennai  Female     100
4  Jitendra   52  Bangalore    Male      41
5   Krishna   34    Chennai    Male      89

DataFrame after replacing City value Hyderabad to Mumbai
       Title  Age       City  Gender  Rating
0   Krishna   34     Mumbai    Male      67
1     Sailu   35  Hyderabad  Female      43
2      Joel  234  Hyderabad    Male      67
3     Chamu   35    Chennai  Female     100
4  Jitendra   52     Mumbai    Male      41
5   Krishna   34    Chennai    Male      89

Dataframe after replacing the dictionary of values
       Title  Age     City  Gender  Rating
0      Hari   34   Mumbai    Male      67
1     Sailu   35    Delhi  Female      43
2      Joel  234    Delhi    Male      67
3     Chamu   35  Chennai  Female     100
4  Jitendra   52   Mumbai    Male      41
5      Hari   34  Chennai    Male      89




 

Previous                                                 Next                                                 Home

No comments:

Post a Comment