how to rename columns in pandas dataframe using str.contains and if statements in python

To rename columns in pandas dataframe using str.contains and if statements in Python:

  1. Create a dictionary with old column names as keys and new column names as values.
  2. Loop through the dictionary items and check if each old column name contains the specified string using str.contains.
  3. If the old column name contains the string, rename the column using rename(columns) function.

Here's an example code:

main.py
import pandas as pd

# Create a sample dataframe
df = pd.DataFrame({'ABC_x': [1, 2, 3], 'DEF_y': [4, 5, 6], 'GHI_z': [7, 8, 9]})

# Define dictionary with old and new column names
new_names = {'ABC_x': 'New_ABC', 'DEF_y': 'New_DEF', 'GHI_z': 'New_GHI'}

# Loop through the dictionary and rename columns
for old_name, new_name in new_names.items():
    if df.columns.str.contains(old_name).any():
        df = df.rename(columns={old_name: new_name})

print(df.head())
467 chars
15 lines

Output:

main.py
   New_ABC  New_DEF  New_GHI
0        1        4        7
1        2        5        8
2        3        6        9
116 chars
5 lines

In this example, the code renames columns that contain ABC_x, DEF_y, and GHI_z with New_ABC, New_DEF, and New_GHI respectively. df.columns.str.contains() function checks if the specified string is present in any column names. If the condition is true, rename() function renames the column with the new name.

related categories

gistlibby LogSnag