I have a question in regards to finding duplicates in a dataframe, and removing duplicates in a dataframe using a specific column.
Here is what I am trying to accomplish:
Is it possible to remove duplicates but keep the first 2?
Here is an example of my current dataframe called df and take a look at the bracket notes I have placed below to give you an idea.
Note: If 'Roll' = 1 then I want to look at the Date column, see if there is a second duplicate Date in that column... keep those two and delete any others.
Date Open High Low Close Roll Dupes
1 19780106 236.00 237.50 234.50 235.50 0 NaN
2 19780113 235.50 239.00 235.00 238.25 0 NaN
3 19780120 238.00 239.00 234.50 237.00 0 NaN
4 19780127 237.00 238.50 235.50 236.00 1 NaN (KEEP)
5 19780203 236.00 236.00 232.25 233.50 0 NaN (KEEP)
6 19780127 237.00 238.50 235.50 236.00 0 NaN (KEEP)
7 19780203 236.00 236.00 232.25 233.50 0 NaN (DELETE)
8 19780127 237.00 238.50 235.50 236.00 0 NaN (DELETE)
9 19780203 236.00 236.00 232.25 233.50 0 NaN (DELETE)
This is what is currently removing the dupes BUT it's removing all dupes (obviously)
df = df.drop_duplicates('Date')
EDIT: I forgot to mention something, the only duplicate I want to keep is if column 'Roll' = 1
if it does, then keep that row and the next one that matches based on column 'Date'
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…