简体   繁体   English

如何计算 Pandas 库中的获胜百分比?

[英]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 HomeTeamName AwayTeamName HomeTeamGoals AwayTeamGoals 0 法国 墨西哥 4.0 1.0 1 美国 比利时 3.0 0.0 2 南斯拉夫 巴西 2.0 1.0 3 罗马尼亚 秘鲁 3.0 1.0 4 阿根廷 法国 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.百分比是通过将值除以所有值的总和然后将总和乘以 100 的数学公式计算的。这也适用于 Pandas 数据帧。 Here, the pre-defined sum() method of pandas series is used to compute the sum of all the values of a column.在这里,pandas 系列的预定义 sum() 方法用于计算列的所有值的总和。

Syntax: Series.sum()语法: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 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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