Search code examples
pythonpandasstrip

Lowercase and replace unwanted substrings from dataframe header in Pandas


I want to lowercase and remove first s from the header of a dataframe: 'id', 'sTitle', 'sSource', 'sDate', 'sAbstract', 'sStart_Time', 'sSite', 'sUrl'.

The expected header will like this: 'id', 'title', 'source', 'date', 'abstract', 'start_time', 'site', 'url'

I have tried with: df.columns = df.columns.str.lower().lstrip('s'), it generate an error: AttributeError: 'Index' object has no attribute 'lstrip'.

How can I do that correctly? Thank you.


Solution

  • You need to write .str again:

    df.columns = df.columns.str.lstrip('s').str.lower()
    

    Edit:

    After discussing with the OP, this was the final working code:

    cols = ['sAbstract', 'sStart_Time']
    df.columns = pd.Series(df.columns.tolist()).replace({k: k.lstrip('s').lower() for k in cols})