Python – Ignoring NaN in a DataFrame with Pandas

dataframenumpypandaspython

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 i do to just ignore the missing values.
dataframe look like this.click here

Best Answer

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.])
Related Question