aeapen
aeapen

Reputation: 923

How to rename dataframe columns in specific way in Python

I have dataframe (df) with column names as shown below and I want to rename it any specific name

Renaming condition:

  1. Remove the underscore -in the column name
  2. Replace the first letter coming after the - from smallcase to uppercase.

Original Column Name

df.head(1)

risk_num  start_date end_date
 12        12-3-2022  25-3-2022

Expected Column Name

df.head(1)

riskNum  startDate   endDate
 12        12-3-2022  25-3-2022

How can this donein python.

Upvotes: 1

Views: 1145

Answers (3)

Corralien
Corralien

Reputation: 120391

Use str.replace:

# Enhanced by @Ch3steR
df.columns = df.columns.str.replace('_(.)', lambda x: x.group(1).upper())
print(df)

# Output
# risk_num start_date   end_date  very_long_column_name
   riskNum  startDate    endDate  veryLongColumnName
0       12  12-3-2022  25-3-2022                   0

Upvotes: 2

jezrael
jezrael

Reputation: 862511

Use Index.map:

#https://stackoverflow.com/a/19053800/2901002
def to_camel_case(snake_str):
    components = snake_str.split('_')
    # We capitalize the first letter of each component except the first one
    # with the 'title' method and join them together.
    return components[0] + ''.join(x.title() for x in components[1:])

df.columns = df.columns.map(to_camel_case)
print (df)
   riskNum  startDate    endDate
0       12  12-3-2022  25-3-2022

Or modify regex solution for pandas:

#https://stackoverflow.com/a/47253475/2901002
df.columns = df.columns.str.replace(r'_([a-zA-Z0-9])', lambda m: m.group(1).upper(), regex=True)
print (df)
   riskNum  startDate    endDate
0       12  12-3-2022  25-3-2022

Upvotes: 2

David
David

Reputation: 891

The following code will do that for you

df.columns = [x[:x.find('_')]+x[x.find('_')+1].upper()+x[x.find('_')+2:] for x in df.columns]

Upvotes: 1

Related Questions