I am trying to remove whitespace from the titles of columns on a dataframe.
my_df=pd.DataFrame({' name_1':[1, 2],' name_2':[3, 4],})
After some research, i've tried:
my_df.columns.map(lstrip())
df.columns.to_series().map(lstrip)
these both give:
NameError: name 'lstrip' is not defined
even though mystr.lstrip() works fine.
how can I do this without getting the name error? and why am I getting it?
CodePudding user response:
Try:
my_df.columns = my_df.columns.str.strip()
CodePudding user response:
lstrip
is a method of the str
class, therefore lstrip()
alone is going to produce that error while str.lstrip()
or mystr.lstrip()
(whit mystr being a string) won't.
So, you can use
my_df.columns.map(str.lstrip)
but because pandas has vecorized versions of the str methods under pandas.Series.str
, is considered more pythonic (pandastic?) to use instead my_df.columns.str.lstrip()
.
As noted by @saad_saeed in their comment, using .strip()
instead of .lstrip()
is recommended to eliminate both trailing and leading whitespaces, which are a common source of KeyError
.