0

I want to make all strings lower case and remove the whitespaces at the beginning and end of the strings.

df = pandas.DataFrame(data=[1,2,3,'A'],columns=['A'])
df['A'] = numpy.where(
    df['A'].apply(lambda x: isinstance(x, str)),
    df['A'].str.lower().str.strip(),
    df['A'],
)

The problem is that the code above fails if none of the rows is a string.

df = pandas.DataFrame(data=[1,2,3],columns=['A'])
AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas

Is there a better way to do this than

for index in df['A'].index:
    if isinstance(df['A'].iloc[index], str):
        df['A'].iloc[index] = df['A'].iloc[index].str.lower().str.strip()
2
  • 1
    I think it is ugly bug :( Commented Feb 21, 2019 at 13:56
  • Use df['A']=df['A'].apply(lambda x: x.lower().strip() if isinstance(x, str) else x) Commented Feb 21, 2019 at 13:57

1 Answer 1

5

Assuming you want to leave your non strings untouched, you can use:

df['A']=df['A'].apply(lambda x: x.lower().strip() if isinstance(x, str) else x) 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.