2017-01-23 3 views
1

私はこのようになりますパンダDATAFRAME dfを持っている:複数のエントリを持つ列のみでDataFrameをサブセット化する方法はありますか。

私は列 1で複数の値を持つ行のみで dfをサブセットしたい
0  1 
C1 V1 
C2 V1 
C3 V1 
C4 V2 
C5 V3 
C6 V3 
C7 V4 

、所望の出力ビーイング:

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

方法私はこれをするのですか?

+1

:http://stackoverflow.com/questions/11528078/determining -duplicate-values-in-an-array – EdChum

答えて

1

私はあなたがTrueとしてすべての重複マークのためkeep=FalseDataFrame.duplicatedによって作成されたマスクでboolean indexingが必要だと思う:関連

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 
関連する問題