How to lowercase a pandas dataframe string column if it has missing values? How to lowercase a pandas dataframe string column if it has missing values? python python

How to lowercase a pandas dataframe string column if it has missing values?


use pandas vectorized string methods; as in the documentation:

these methods exclude missing/NA values automatically

.str.lower() is the very first example there;

>>> df['x'].str.lower()0    one1    two2    NaNName: x, dtype: object


Another possible solution, in case the column has not only strings but numbers too, is to use astype(str).str.lower() or to_string(na_rep='') because otherwise, given that a number is not a string, when lowered it will return NaN, therefore:

import pandas as pdimport numpy as npdf=pd.DataFrame(['ONE','Two', np.nan,2],columns=['x']) xSecureLower = df['x'].to_string(na_rep='').lower()xLower = df['x'].str.lower()

then we have:

>>> xSecureLower0    one1    two2   3      2Name: x, dtype: object

and not

>>> xLower0    one1    two2    NaN3    NaNName: x, dtype: object

edit:

if you don't want to lose the NaNs, then using map will be better, (from @wojciech-walczak, and @cs95 comment) it will look something like this

xSecureLower = df['x'].map(lambda x: x.lower() if isinstance(x,str) else x)


you can try this one also,

df= df.applymap(lambda s:s.lower() if type(s) == str else s)