Skip to content
Advertisement

New column adding values of different columns with strings and numbers

I have a dataframe like this structure (in the real one there are more columns Game x, around 30, but for explaining I think it’s ok with these 2 columns):

JavaScript

I need new columns for counting the minutes of any player in the columns “Game x” based in these conditions:

  • Starting: means the player has played 90 minutes
  • Starting 68 (or whichever): means the player has played 68 minutes (or whichever)
  • Bench and NaN: means the player has played 0 minutes
  • Bench 74 (or whichever): means the player has played 16 minutes (the total is 90 so he started at the minute 74 and then is 90 – 74 = 16)

There would be 2 columns counting the number of the minutes the player has played when he started the game and when he entered the game from the bench.

The final dataframe would be:

JavaScript

Advertisement

Answer

If you write a function that parses a text field and returns the corresponding number of minutes, you can apply that function to each game column and add up the results. For example, the time played from start:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement