Home > Back-end >  How can I fill a column based on a difference between the values of two different columns, using gro
How can I fill a column based on a difference between the values of two different columns, using gro

Time:11-14

I have the following data frame:

HorseId FGrating Max FGrating Top
1736 110 No (no Max FGrating to compare with)
1736 124 110 Yes (FGrating - Max FGrating>=4)
1736 118 124 No (FGrating - Max FGrating <4)
13973 144 No (no Max FGrating to compare with)
13973 137 144 No (FGrating - Max FGrating <4)

The Top column is "Yes" or "1" when the difference between FGrating and MaxFGrating for that row is 4 or more.

I know I should use groupby, because I need this for each HorseId and perhaps the diff function, but I cannot put them together.

How can I do it?

CodePudding user response:

Your expected output is hard to make sense of, because the values in Top do not seem driven by any conditions in the table, except those two rows with MaxFGrating == -1 seem to be consistently MaxFGrating - FGrating.

>>> ((df['FGrating'] - df['Max FGrating']) >= 4).map({True:'Yes', False:'No'})
0    Yes
1    Yes
2     No
3    Yes
4     No
dtype: object

Or MaxFGrating - FGrating.

>>> ((df['Max FGrating'] - df['FGrating']) >= 4).map({True:'Yes', False:'No'})
0     No
1     No
2    Yes
3     No
4    Yes
dtype: object
  • Related