Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Panda dataframe making every unique ID number NAT

I have a dataframe, and for every unqiue ID, make the first 'Diff' column NaT, for example my data starts off looking like

index   DEVICE_ID      DIFF
0        12             Nat
1        12              20
2        12              30
3        13              40
4        13              40
5        13              21
6        14               9
7        14              10    

But I want the resulting dataframe to look like the one below

index   DEVICE_ID      DIFF
0        12             Nat
1        12              20
2        12              30
3        13             Nat
4        13              40
5        13              21
6        14             Nat
7        14              10

Sorry for the poorly worded question

like image 634
samari708 Avatar asked Feb 03 '26 11:02

samari708


1 Answers

Using df.duplicated() and df.loc

df.loc[~df.DEVICE_ID.duplicated(),'DIFF'] = pd.NaT
>>df

   index  DEVICE_ID DIFF
0      0         12  NaT
1      1         12   20
2      2         12   30
3      3         13  NaT
4      4         13   40
5      5         13   21
6      6         14  NaT
7      7         14   10
like image 53
anky Avatar answered Feb 06 '26 03:02

anky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!