1

I have a pandas DataFrame df that looks like this:

0     1
C1    V1
C2    V1
C3    V1
C4    V2
C5    V3
C6    V3
C7    V4

I wish to subset df by only those rows that have multiple values in column 1, the desired output being:

0     1
C1    V1
C2    V1
C3    V1
C5    V3
C6    V3

How do I do this?

1

1 Answer 1

1

I think you need boolean indexing with mask created by DataFrame.duplicated with keep=False for mark all duplicates as True:

print (df.columns)
Index(['0', '1'], dtype='object')

mask = df.duplicated('1', keep=False)
#another solution with Series.duplicated
#mask = df['1'].duplicated(keep=False)

print (mask)
0     True
1     True
2     True
3    False
4     True
5     True
6    False
dtype: bool

print (df[mask])
    0   1
0  C1  V1
1  C2  V1
2  C3  V1
4  C5  V3
5  C6  V3

print (df.columns)
Int64Index([0, 1], dtype='int64')

mask = df.duplicated(1, keep=False)
#another solution with Series.duplicated
#mask = df[1].duplicated(keep=False)

print (mask)
0     True
1     True
2     True
3    False
4     True
5     True
6    False
dtype: bool

print (df[mask])
    0   1
0  C1  V1
1  C2  V1
2  C3  V1
4  C5  V3
5  C6  V3
Sign up to request clarification or add additional context in comments.

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.