Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 862
mihir shanvir Avatar asked Jun 21 '17 16:06

mihir shanvir


People also ask

How do I ignore NaN values?

logical_not() function around the output of the isnan() function. We do that because we want the non-NaN values to be printed into the new array. By using logical_not(), it will convert the False values into True and vice – versa. So, for non-NaN values, the value will be True, and for NaN values, it will be false.

How do I skip NaN in pandas?

Use dropna() function to drop rows with NaN / None values in pandas DataFrame.

How do you exclude NaN in Python?

To remove NaN from a list using Python, the easiest way is to use the isnan() function from the Python math module and list comprehension. You can also use the Python filter() function. The Python numpy module also provides an isnan() function that we can use to check if a value is NaN.

Does DataFrame mean ignore NaN?

DataFrame. mean() function is used to get the mean of the values over the requested axis in pandas. This by default returns a Series, if level specified, it returns a DataFrame. By default ignore NaN values and performs mean on index axis.


1 Answers

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.])
like image 101
Peter Leimbigler Avatar answered Oct 26 '22 11:10

Peter Leimbigler