Hi So I have a data frame with column names that end in '2018'
I need to remove the years from these column names and am having some trouble. I also need to strip leading and trailing spaces from these column names as well.
I've already tried the following:
df.columns.str.replace('\d+',"") #to try and remove the numbers from the column names
df.columns = df.columns.str.strip('') #to try and get rid of the spaces
These do nothing to the dataframe.
I expect the column names to go from " Stock 2018" to "Stock"
but this isn't happening. Thank you for the help!
You can try using regex as well..
Example DataFrame:
Result using
regex: