How to overcome 'NoneType' object has no attribute 'lower' error?

  • A+
Category:Languages

I have a dataframe df:

df=pd.DataFrame({'id':[1,2,3],'item1':['AK','CK',None], 'item2':['b','d','e'],'item3':['c','e',np.nan]}) 

I want to convert all the values of column item1 into lowercase.

I tried:

df['item1'].apply(lambda x: x.lower()) 

which is giving me error :

AttributeError: 'NoneType' object has no attribute 'lower'

I know why it is happening. One of my column value is None.

I want to anyhow ignore that value and convert the rest of the values into lowercase.

Is there a way to overcome this?

P.S: My original dataframe may have any number of values as it is returned by another function. Dropping the row is not a case here as those records are important for me.

 


More general solution for None and NaNs values is use notnull function, if performance is important use list comprehension.

Also pandas string functions working very nice with None and NaNs:

df['new1'] = df['item1'].apply(lambda x: x.lower() if pd.notnull(x) else x)  df['new2'] = [x.lower() if pd.notnull(x) else x for x in df['item1']]  df['new3'] = df['item1'].str.lower() print (df)    id item1 item2 item3  new1  new2  new3 0   1    AK     b     c    ak    ak    ak 1   2    CK     d     e    ck    ck    ck 2   3  None     e   NaN  None  None  None 

df=pd.DataFrame({'id':[1,2,3],'item1':['AK',np.nan,None], 'item2':['b','d','e'],'item3':['c','e',np.nan]}) print (df)    id item1 item2 item3 0   1    AK     b     c 1   2   NaN     d     e 2   3  None     e   NaN  df['new1'] = df['item1'].apply(lambda x: x.lower() if pd.notnull(x) else x) df['new2'] = [x.lower() if pd.notnull(x) else x for x in df['item1']] df['new3'] = df['item1'].str.lower() print (df)    id item1 item2 item3  new1  new2  new3 0   1    AK     b     c    ak    ak    ak 1   2   NaN     d     e   NaN   NaN   NaN 2   3  None     e   NaN  None  None  None 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: