Python pandas lower data AttributeError: 'Series' object has no attribute 'lower'

Question:

I want to lower data taken from pandas sheet and trim all spaces then to look for an equality.

df['ColumnA'].loc[lambda x: x.lower().replace(" ", "") == var_name]

Code is above.
It says pandas series has no lower method. But I need to search for data inside column A via pandas framework while lowering all letters to small and whitespace trimmering.
Any other idea, how can I achieve in pandas?

Asked By: xlmaster

||

Answers:

In your lambda function, x is a Series not a string so you have to use str accessor:

df['ColumnA'].loc[lambda x: x.str.lower().replace(" ", "") == var_name]

Another way:

df.loc[df['ColumnA'].str.lower().str.replace(' ', '') == var_name, 'ColumnA']
Answered By: Corralien
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.