Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Replacing the first occurrence of a text in each row of a pandas DataFrame based on an ID

I am trying to replace the first occurance based on the ID. My dataset looks like this:

df=

Index     ID             Status  
0     1895001            review   
1     1895001            review      
2     1895001            review      
3     2104264            review        
4     2102404            review        
5     2102404            review         
6     1809905            review
7     1809905            review       
8     1809905            review      
9     1811700            review

I tried this df.values[df.index, np.argmax(df.values=="review",1)] = "first review", but it replaces all of them 🙁

This is what I am expecting:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df=

Index   ID                   Status
0    1895001             first review         
1    1895001             review      
2    1895001             review       
3    2104264             first review        
4    2102404             first review        
5    2102404             review         
6    1809905             first review
7    1809905             review       
8    1809905             review       
9    1811700             first review

>Solution :

Use boolean indexing with the boolean inverse (~) of duplicated:

df.loc[~df['ID'].duplicated(), 'Status'] = 'first review'

Output:

   Index       ID        Status
0      0  1895001  first review
1      1  1895001        review
2      2  1895001        review
3      3  2104264  first review
4      4  2102404  first review
5      5  2102404        review
6      6  1809905  first review
7      7  1809905        review
8      8  1809905        review
9      9  1811700  first review
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading