Home > other >  Renaming columns based on condition in Pandas with lstrip is not working
Renaming columns based on condition in Pandas with lstrip is not working

Time:07-08

I have the following dataframe:

df2 = pd.DataFrame({'date' : ['2020-08-01', '2020-09-01', '2020-10-01'], 'd1_ads' : [3, 5, 6],'d7_ads' : [8, 9, 15], 'd30_ads' : [12, 10, 22]})

And I want to remove the first letter d from the columns d1_ads, d7_ads and d30_ads. The first column date should remain like that, with the letter d. I have tried this code to achieve this:

df2.iloc[:,1:3].columns.str.lstrip("d")

And this is what I get:

Index(['1_iap', '7_iap', '30_iap',],
      dtype='object')

But when I try to save it on the dataframe, it is not working:

df2.iloc[:,1:3].columns = df2.iloc[:,1:3].columns.str.lstrip("d")

And my columns remain the same:

df2 = pd.DataFrame({'date' : ['2020-08-01', '2020-09-01', '2020-10-01'], 'd1_ads' : [3, 5, 6],'d7_ads' : [8, 9, 15], 'd30_ads' : [12, 10, 22]})

This would be the expected output:

df2 = pd.DataFrame({'date' : ['2020-08-01', '2020-09-01', '2020-10-01'], '1_ads' : [3, 5, 6],'7_ads' : [8, 9, 15], '30_ads' : [12, 10, 22]})

What am I doing wrong? It seems it is not saving the changes correctly.

Thanks

CodePudding user response:

Use df.rename(...) to rename columns. You'll have to create a dict which maps the name: {'old': 'new'}

df2.rename(columns={i: i.lstrip("d") for i in df2.iloc[:,1:4].columns},
           inplace=True)
# print(df2.to_markdown())
date 1_ads 7_ads 30_ads
0 2020-08-01 3 8 12
1 2020-09-01 5 9 10
2 2020-10-01 6 15 22

CodePudding user response:

You can try lstrip the last three column names then add the first column name or with .str.replace

df2.columns = df2.columns[:1].tolist()   df2.columns[1:].str.lstrip("d").tolist()
# or
df2.columns = df2.columns.str.replace('^d(\d.*)', r'\1', regex=True)
  • Related