I have a pandas dataframe where the column names are capital and snake case. I want to convert them into camel case with first world starting letter to be lower case. The following code is not working for me. Please let me know how to fix this.
import pandas as pd
# Sample DataFrame with column names
data = {'RID': [1, 2, 3],
'RUN_DATE': ['2023-01-01', '2023-01-02', '2023-01-03'],
'PRED_VOLUME_NEXT_360': [100, 150, 200]}
df = pd.DataFrame(data)
# Convert column names to lowercase
df.columns = df.columns.str.lower()
# Convert column names to camel case with lowercase starting letter
df.columns = [col.replace('_', ' ').title().replace(' ', '').replace(col[0], col[0].lower(), 1) for col in df.columns]
# Print the DataFrame with updated column names
print(df)
I want to column names RID, RUN_DATE, PRED_VOLUME_NEXT_360 to be converted to rid, runDate, predVolumeNext360, but the code is giving Rid, RunDate and PredVolumeNext360.
You could use a regex to replace
_x
by_X
:Or with a custom function:
Output: