Home > front end >  Filter a pandas document using row and column keys
Filter a pandas document using row and column keys

Time:04-27

I have a csv document that looks like the following one:

_id_ name score_1 score_2 score_3
5254454 John 10 8 9
5435454 Chris 8 6 7
1233123 John 5 5 6
65566565 Jim nan nan nan
... ... ... ...

Most of the rows do not have a value for the score_1 column, so I would like to access them and fill a grade.

So, I would like to filter the score_1 of a specific person using his id or name. So if the dataframe name is df I am trying to do something like that:

df.loc[df['_id_']==int(id1)] # which actually filters a row

Then, I would like to filter this row with the key of score_1 and add a value and in the end, save the updated dataframe to CSV. How can I do that?

I have tried to do sth like that but it failed:

df.loc[df['_id_']==int(id1)][df['score_1']] = 10

CodePudding user response:

Try this:

df.loc[df['_id_']==int(id1), 'score_1'] = 10
  • Related