How to create a ratio score from values in a pandas DataFrame?

Christian Magelssen

I have DataFrame that looks like this:

   BIB#            COURSE  FINISH
0     1          COURSE 1  21.235
1     1          COURSE 2  23.345
2     1  STRAIGHT-GLIDING  20.690
3     2          COURSE 1  20.220
4     2          COURSE 2  22.535
5     2  STRAIGHT-GLIDING  19.910

Now I want to calculate a ratio score for each BIB#. The ratio score should look like this:

PERFORMANCE = COURSE 1 + COURSE 2 / STRAIGHT-GLIDING

The values for this calculation is found in the FINISH column.

I think I can create a function that loops over each row in the DataFrame but I guess there is a better way to do it using Pandas? Thanks!

(If I were to create the function, then I would loop through each BIB# with if tests)

jezrael

Use DataFrame.pivot, then add and divide columns each other:

df1 = df.pivot('BIB#','COURSE','FINISH')
df1['PERFORMANCE'] = df1['COURSE 1'].add(df1['COURSE 2']).div(df1['STRAIGHT-GLIDING'])
print (df1)
COURSE  COURSE 1  COURSE 2  STRAIGHT-GLIDING  PERFORMANCE
BIB#                                                     
1         21.235    23.345             20.69     2.154664
2         20.220    22.535             19.91     2.147413

If need same format like original add DataFrame.stack with Series.reset_index:

df2 = df1.stack().reset_index(name='FINISH')
print (df2)
   BIB#            COURSE     FINISH
0     1          COURSE 1  21.235000
1     1          COURSE 2  23.345000
2     1  STRAIGHT-GLIDING  20.690000
3     1       PERFORMANCE   2.154664
4     2          COURSE 1  20.220000
5     2          COURSE 2  22.535000
6     2  STRAIGHT-GLIDING  19.910000
7     2       PERFORMANCE   2.147413

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to calculate ratio of values in a pandas dataframe column?

From Dev

Create a new column that stores a ratio score in Pandas

From Dev

How to create Pandas DataFrame with single values from another DataFrame?

From Dev

Pandas - how to create a new dataframe from the columns and values of an old dataframe?

From Dev

How to create a pandas dataframe from two dictionaries with same key values?

From Dev

How to create images from the row values in a pandas dataframe, using PIL?

From Dev

How to create a pandas dataframe of unique values fetched from column with no duplicates

From Dev

How to create lists from definite cell values of a Pandas dataframe?

From Dev

How to create a Frequency Distribution Matrix from a Pandas DataFrame of boolian values

From Dev

How to create a new column in a pandas dataframe based on values from a loop?

From Dev

How to calculate ratio from two different pandas dataframe

From Dev

Create a table of values from a pandas DataFrame

From Dev

Create a list of tuples from pandas DataFrame values

From Dev

How to create Pandas dataframe of given values

From Dev

How to create new pandas DataFrame with group by values?

From Dev

How to create new values in a pandas dataframe column based on values from another column

From Dev

Pandas DataFrame: How do I create numerical values out of numerical values from another column?

From Dev

Create a new dataframe from the top values of another dataframe in Pandas

From Dev

How to update the empty dataframe values with values from another dataframe(Pandas)?

From Dev

How to create a dataframe from the values in a logfile?

From Dev

How to create a DataFrame from custom values

From Dev

How to get a set of values from a Pandas dataframe?

From Dev

How to recursively extract values from a pandas DataFrame?

From Dev

How to create a new dataframe column using values and groupings from other rows and columns in pandas?

From Dev

How do I create a column in a pandas dataframe using values from two rows?

From Python

How to create new rows from values inside in a cloumn of pandas dataframe based on delimeter in Python?

From Python

How to create a new column based on values from other columns in a Pandas DataFrame

From Dev

Python/Pandas: How to create a table of results with new variables and values calculated from an existing dataframe

From Dev

How to create column with values from column of duplicated rows separated by commas in DataFrame in Python Pandas?

Related Related

  1. 1

    How to calculate ratio of values in a pandas dataframe column?

  2. 2

    Create a new column that stores a ratio score in Pandas

  3. 3

    How to create Pandas DataFrame with single values from another DataFrame?

  4. 4

    Pandas - how to create a new dataframe from the columns and values of an old dataframe?

  5. 5

    How to create a pandas dataframe from two dictionaries with same key values?

  6. 6

    How to create images from the row values in a pandas dataframe, using PIL?

  7. 7

    How to create a pandas dataframe of unique values fetched from column with no duplicates

  8. 8

    How to create lists from definite cell values of a Pandas dataframe?

  9. 9

    How to create a Frequency Distribution Matrix from a Pandas DataFrame of boolian values

  10. 10

    How to create a new column in a pandas dataframe based on values from a loop?

  11. 11

    How to calculate ratio from two different pandas dataframe

  12. 12

    Create a table of values from a pandas DataFrame

  13. 13

    Create a list of tuples from pandas DataFrame values

  14. 14

    How to create Pandas dataframe of given values

  15. 15

    How to create new pandas DataFrame with group by values?

  16. 16

    How to create new values in a pandas dataframe column based on values from another column

  17. 17

    Pandas DataFrame: How do I create numerical values out of numerical values from another column?

  18. 18

    Create a new dataframe from the top values of another dataframe in Pandas

  19. 19

    How to update the empty dataframe values with values from another dataframe(Pandas)?

  20. 20

    How to create a dataframe from the values in a logfile?

  21. 21

    How to create a DataFrame from custom values

  22. 22

    How to get a set of values from a Pandas dataframe?

  23. 23

    How to recursively extract values from a pandas DataFrame?

  24. 24

    How to create a new dataframe column using values and groupings from other rows and columns in pandas?

  25. 25

    How do I create a column in a pandas dataframe using values from two rows?

  26. 26

    How to create new rows from values inside in a cloumn of pandas dataframe based on delimeter in Python?

  27. 27

    How to create a new column based on values from other columns in a Pandas DataFrame

  28. 28

    Python/Pandas: How to create a table of results with new variables and values calculated from an existing dataframe

  29. 29

    How to create column with values from column of duplicated rows separated by commas in DataFrame in Python Pandas?

HotTag

Archive