Posts

Showing posts from October, 2022

How to update a field value in a specific column by id from another column - Pandas Tips

Image
Hello, i'm creating this blog with some tips that i took a long time to find to some specific problems How do I update a field value from some column searching by id from a column in Pandas? This is similar to a query in SQL like: UPDATE tableX SET fieldX = 'valueX' WHERE ID = 'idX' So: yourDF.loc[yourDF['ColumnNameWhereIsYourId'].isin( [HERE YOU CAN PUT A LIST OF ID's or just one like '67ae47e2-f04d-415e-a2b5-2c212388044a']), 'NameOfColumnThatYouHaveToUpdateTheValue'] = 'Value you want' Example: I have a dataframe pandas like this: I wanna change name jhon from id 67ae47e2-f04d-415e-a2b5-2c212388044a to Jonathan. So i will do this: df.loc[df['column_id'].isin( ['67ae47e2-f04d-415e-a2b5-2c212388044a']), 'ColumnName'] = 'Jonathan' My dataframe now: This will update a value from a column in the row that your ID is in.