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
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.
Use dropna() function to drop rows with NaN / None values in pandas DataFrame.
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.
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.
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.])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With