I would like to do the following:
If two rows have exactly the same value in 3 columns ("ID","symbol", and "date") and have either "X" or "T" in one column ("message"), then remove both of these rows. However, if two rows have the same value in the same 3 columns but a value different than "X" or "T" in the other column, then leave intact.
Here is an example of my data frame:
df = pd.DataFrame({"ID":["AA-1", "AA-1", "C-0" ,"BB-2", "BB-2"], "symbol":["A","A","C","B","B"], "date":["06/24/2014","06/24/2014","06/20/2013","06/25/2014","06/25/2015"], "message": ["T","X","T","",""] })
Note that the first two rows have the same value values for the columns "ID","symbol", and "date", and "T" and "X" in the column "message". I would like to remove these two rows.
However, the last two rows have the same value in columns "ID","symbol", and "date", but blank (different than "X" or "T") in the column "message".
I am interested in applying the function to a large dataset with several million rows. So far what I have tried consumes all my memory,
thank you and I appreciate any help,
I think you can use
groupby
withfilter
- conditions are - not2
rows with duplicate values and columnmessage
in groupsisin
have not valuesT
orX
:Filtration in docs.
EDIT by comment:
If need remove values with
X
orT
in each group - it means it remove doubleX
or doubleT
too and eachlen
of each group is always2
:If need remove only groups where are values
T
andX
, you can firstsort_values
bymessage
and thenfilter
by checking if first value isT
and secondX
in each group. ('T' is first andX
is second, because sorting):This might work for you:
It's reasonably fast:
The alternative was giving indexing errors.