Home > Software engineering >  Python, Pandas, remove n-letters of string without removing index/title
Python, Pandas, remove n-letters of string without removing index/title

Time:07-25

this is probably a easy fix that just eludes me right now. I have a excel file with the following content.

enter image description here

from it I want to filter out the "Num-" from the rest. For simplicitys sake I use .str here.

df_test = pd.read_excel(r'C:\...\test.xlsx')
df_test = df_test.filter(like='Order_Number', axis=1)
df_test = df_test['Order_Number'].str[4:]
df_test.head()

The output comes out without the title Order_Number though and I am not sure why. How can I preserve it without adding it manually back?

enter image description here

CodePudding user response:

It appears you are assigning the new values of 'Order_Number' column to entire dataframe, instead of assigning them to the actual column. Try:

df_test['Order_Number'] = df_test['Order_Number'].str[4:]
  • Related