python 3.x - Pandas combines values from two columns and compare for uniqueness -
i want combine/concatenate values 2 columns of dataframe , compare uniqueness of these values, e.g.
col1 col2 row1 val11 val12 row2 val21 val22 row3 val31 val32
i want concatenate val11
, val12
, val21
, val22
, val31
, val32
, compare val11+val12
, val21+val22
, val31+val32
uniqueness, i.e. check if 3 concatenated values equal.
the dtype
of col1
, col2
str
.
i wondering whats best way this.
you can use duplicated
checking uniqueness of concanecated columns col1
, col2
boolean indexing
:
print df col1 col2 row1 val11 val12 row2 val21 val22 row3 val31 val32 row3 val31 val32 ser = df.col1 + df.col2 print ser row1 val11val12 row2 val21val22 row3 val31val32 row3 val31val32 print ser.duplicated(keep=false) dtype: object row1 false row2 false row3 true row3 true print ~ser.duplicated(keep=false) row1 true row2 true row3 false row3 false dtype: bool print df[~ser.duplicated(keep=false)] col1 col2 row1 val11 val12 row2 val21 val22
Comments
Post a Comment