Skip to content Skip to sidebar Skip to footer

Ignoring Nan In A Dataframe

I want to find the unique elements in a column of a dataframe which have missing values. i tried this: df[Column_name].unique() but it returns nan as one of the elements. what can

Solution 1:

Try calling .dropna() right before your call to .unique(). A working example:

import pandas as pd
import numpy as np
df = pd.DataFrame({'col1': np.random.randint(0, 10, 12)})
df.loc[2] = np.nan
df.loc[5] = np.nan
df['col1'].unique()
### output: array([  4.,   0.,  nan,   8.,   1.,   3.,   2.,   6.])
df['col1'].dropna().unique()
### output: array([ 4.,  0.,  8.,  1.,  3.,  2.,  6.])

Post a Comment for "Ignoring Nan In A Dataframe"