3

Here's an example:

    A    
0   V1 
1   V1 
2   V2 
3   v4  
4   v4 
5   v7 

And I would want to end up with something like this:

    A    
0   V1 
1   V1 
2   np.NaN 
3   v4  
4   v4 
5   np.NaN

Basically it would be something like this:

df.A[df.A.value_counts() < 2] = np.NaN

2 Answers 2

3

Use groupby + transform, and then index with loc -

df.loc[df.groupby('A').A.transform('count').lt(2), 'A'] = np.nan    
df

     A
0   V1
1   V1
2  NaN
3   v4
4   v4
5  NaN
Sign up to request clarification or add additional context in comments.

1 Comment

@BryceSoker No problem. Happy coding.
3

Use value_counts and then check values of index byisin :

a = df.A.value_counts()
m = df.A.isin(a.index[a<2])
print (m)
0    False
1    False
2     True
3    False
4    False
5     True
Name: A, dtype: bool

df.loc[m, 'A'] = np.NaN
print (df)
     A
0   V1
1   V1
2  NaN
3   v4
4   v4
5  NaN

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.