简体   繁体   中英

How to Calculate Win Percentage In Pandas Library?

HomeTeamName AwayTeamName HomeTeamGoals AwayTeamGoals 0 France Mexico 4.0 1.0 1 USA Belgium 3.0 0.0 2 Yugoslavia Brazil 2.0 1.0 3 Romania Peru 3.0 1.0 4 Argentina France 1.0 0.0

A Percentage is calculated by the mathematical formula of dividing the value by the sum of all the values and then multiplying the sum by 100. This is also applicable in Pandas Dataframes. Here, the pre-defined sum() method of pandas series is used to compute the sum of all the values of a column.

Syntax: Series.sum()

Return: Returns the sum of the values.

Formula:

df[percent] = (df['column_name'] / df['column_name'].sum()) * 100 for example # Import required libraries import pandas as pd import numpy as np

# Dictionary
df1 = {
 'Name': ['abc', 'bcd', 'cde',
         'def', 'efg', 'fgh',
         'ghi'],
 'Math_score': [52, 87, 49,
               74, 28, 59,
               48]}

 # Create a DataFrame
df1 = pd.DataFrame(df1, 
               columns = ['Name',
                         'Math_score'])

# Calculating Percentage
df1['percent'] = (df1['Math_score'] / 
              df1['Math_score'].sum()) * 100

# Show the dataframe
df1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM