3

I have a dataframe as below:

df = pd.DataFrame({'a': [10, 11, None],
                   'b': ['apple;', None, 'orange;'],
                   'c': ['red', 'blue', 'green']})

I'm trying to strip the ';' of those strings. I tried

df.select_dtypes(include=['object']).applymap(lambda x: x.strip(';'))

I got error message:

AttributeError: ("'NoneType' object has no attribute 'strip'", 'occurred at   index b') 

Seems like the None gave me some trouble. Help is greatly appreciated. Thanks a lot.

4 Answers 4

2

The problem is that some of the values are None, and you can't Non.strip().

df.select_dtypes(include=['object'])
         b      c
0   apple;    red
1     None   blue
2  orange;  green

What you can do is strip only if the object is not None, otherwise just return the object:

df.select_dtypes(include=['object']).applymap(lambda x: x.strip(';') if x else x)
        b      c
0   apple    red
1    None   blue
2  orange  green
Sign up to request clarification or add additional context in comments.

4 Comments

The problem with lambda x: x.strip(';') if x else x is that you will have attribute errors with other objects that are falsey in the Python sense and also lack a .strip method. You can do lambda x: x.strip(';') if hasattr(x, 'strip') else x if you want to do it by testing first.
@dawg, correct. since the data in the example includes only strings the if x was enough. Another option is to check if type(x) == str, to make sure the strip will be only on strings (and not on other objects that might have the strip function, which we are not sure will return the expected result).
this is perfect. Thanks a lot!
@Dekel just did. just knew I need to do this.
1

You can use try and except in this case.

>>> def am(o):
...    try:
...       return o.strip(';')
...    except AttributeError:
...       return o

Then applymap as you have tried:

>>> df.select_dtypes(include=['object']).applymap(am)
        b      c
0   apple    red
1    None   blue
2  orange  green

Comments

0

Use the Series str attribute and apply instead of applymap:

In [17]: df.select_dtypes(include=['object']).apply(lambda S:S.str.strip(';'))
Out[17]: 
        b      c
0   apple    red
1    None   blue
2  orange  green

In [18]: 

Comments

0

A different approach is to iterate through all the columns that are dtype object and use the Series function strip that handles NaN values:

for col in df.columns[df.dtypes == object]:
    df[col] = df[col].str.strip(";")

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.