Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

The following code does not work.

import pandas as pd import numpy as np df=pd.DataFrame(['ONE','Two', np.nan],columns=['x'])  xLower = df["x"].map(lambda x: x.lower()) 

How should I tweak it to get xLower = ['one','two',np.nan] ? Efficiency is important since the real data frame is huge.

like image 813
P.Escondido Avatar asked Mar 07 '14 08:03

P.Escondido


People also ask

How do you deal with missing values in pandas DataFrame?

In order to check missing values in Pandas DataFrame, we use a function isnull() and notnull(). Both function help in checking whether a value is NaN or not. These function can also be used in Pandas Series in order to find null values in a series.

How do I change the DataFrame column names to lowercase in pandas?

We can convert the names into lower case using Pandas' str. lower() function. We first take the column names and convert it to lower case. And then rename the Pandas columns using the lowercase names.


1 Answers

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    one 1    two 2    NaN Name: x, dtype: object 
like image 116
behzad.nouri Avatar answered Sep 19 '22 02:09

behzad.nouri