rename a column in pandas based on str.contains in python

You can use the pandas.DataFrame.rename() method along with a dictionary to rename the columns based on a condition. Here is an example:

main.py
import pandas as pd

# create a sample dataframe
df = pd.DataFrame({'col1': [1, 2, 3],
                   'col2_a': [4, 5, 6],
                   'col2_b': [7, 8, 9],
                   'col3': [10, 11, 12]})

# create a dictionary to map the old column names to new column names
new_names = {}
for col in df.columns:
    if 'col2' in col:
        new_names[col] = 'new_col2_name'
    else:
        new_names[col] = col

# rename the columns using the dictionary
df = df.rename(columns=new_names)

print(df)
508 chars
21 lines

Output:

main.py
   col1  new_col2_name  new_col2_name  col3
0     1              4              7    10
1     2              5              8    11
2     3              6              9    12
176 chars
5 lines

In this example, the condition is checking whether 'col2' is present in the column name. You can modify the condition based on your specific requirement.

related categories

gistlibby LogSnag