📌  相关文章
📜  Pandas中一列的累积百分比 - Python

📅  最后修改于: 2022-05-13 01:55:02.951000             🧑  作者: Mango

Pandas中一列的累积百分比 - Python

累积百分比是通过将列的累积总和除以所有值的数学总和然后将结果乘以 100 的数学公式计算的。这也适用于 Pandas 数据帧。
在这里,预定义的cumsum()sum()函数用于计算列的所有值的累积和和总和。
句法:

示例 1:

Python3
import pandas as pd
import numpy as np
  
# Create a DataFrame
df1 = {
     'Name':['abc','bcd','cde','def','efg','fgh','ghi'],
   'Math_score':[52,87,49,74,28,59,48]}
  
df1 = pd.DataFrame(df1, columns=['Name','Math_score'])
 
# Computing Cumulative Percentage
df1['cum_percent'] = 100*(df1.Math_score.cumsum() / df1.Math_score.sum())
 
df1


Python3
import pandas as pd
import numpy as np
  
# Create a DataFrame
df1 = {
     'Name':['abc','bcd','cde','def','efg','fgh','ghi'],
   'Math_score':[52,87,49,74,28,59,48],
  'Eng_score':[34,67,25,89,92,45,86]
}
  
df1 = pd.DataFrame(df1,columns=['Name','Math_score','Eng_score'])
 
# Computing cumulative Percentage
df1['Eng_cum_percent'] = (df1.Eng_score.cumsum() / df1.Eng_score.sum()) * 100
 
df1


输出:

示例 2:

Python3

import pandas as pd
import numpy as np
  
# Create a DataFrame
df1 = {
     'Name':['abc','bcd','cde','def','efg','fgh','ghi'],
   'Math_score':[52,87,49,74,28,59,48],
  'Eng_score':[34,67,25,89,92,45,86]
}
  
df1 = pd.DataFrame(df1,columns=['Name','Math_score','Eng_score'])
 
# Computing cumulative Percentage
df1['Eng_cum_percent'] = (df1.Eng_score.cumsum() / df1.Eng_score.sum()) * 100
 
df1

输出: