Skip to content Skip to sidebar Skip to footer

Nunique Excluding Some Values In Pandas

I am calculating unique values, per row. However I want to exclude the value 0 and then calculate uniques d = {'col1': [1, 2, 3], 'col2': [3, 4, 0], 'col3': [0, 4, 0],} df = pd.Dat

Solution 1:

To do this you can simply replace zeroes with Nan values.

import pandas as pd
import numpy as np
d = {'col1': [1, 2, 3], 'col2': [3, 4, 0], 'col3': [0, 4, 0]}
df = pd.DataFrame(data=d)
df['uniques'] =  df.replace(0, np.NaN).nunique(axis=1)

Solution 2:

Try this:

def func(x):
    s = set(x)
    s.discard(0)
    returnlen(s)

df['uniq'] = df.apply(lambda x: func(x), axis=1)

Solution 3:

A slightly more concise version without using replace:

df['unique'] = df[df!=0].nunique(axis=1)
df

Output:

   col1  col2  col3  unique013021244223001

Post a Comment for "Nunique Excluding Some Values In Pandas"