如何将 pandas.series.groupby() 表从计数转换为百分比频率



我有一个小程序,我用它来尝试计算范围内序列的百分比频率。我成功地生成了一个包含给定范围内所有值计数的表,但是我找不到一种方法(如果存在(将计数显示为值总数的百分比。我在下面包含了代码,并在下面包含了我当前的输出。

蟒蛇代码:

import numpy as np
import pandas as pd
data = pd.read_excel('HmEqty.xlsx')
bad = data["BAD"]
loan = data["LOAN"]
mortdue = data["MORTDUE"]
value = data["VALUE"]
yoj = data["YOJ"]
derog = data["DEROG"]
delinq = data["DELINQ"]
clage = data["CLAGE"]
ninq = data["NINQ"]
clno = data["CLNO"]
debtinc = data["DEBTINC"]

group = loan.groupby(pd.cut(loan, np.arange((loan.min()-100), 
     (loan.max()+100), 500))).count()
print(group)    

输出:

LOAN
(1600, 2100]     2
(2100, 2600]     3
(2600, 3100]     5
(3100, 3600]     3
(3600, 4100]     8
(4100, 4600]    10
(4600, 5100]    24
(5100, 5600]    23
(5600, 6100]    24
Name: LOAN, dtype: int64

有没有一种方法可以用来显示百分比而不是计数?我是否应该在新数据帧上执行第二个操作?如果是这样,我应该怎么做?

value_countsnormalize一起使用:

s = pd.cut(loan, np.arange((loan.min()-100), (loan.max()+100), 500))
out = s.value_counts(normalize=True)

或:

s1 = loan.groupby(s).size()
out = s1.div(s1.sum())

示例

np.random.seed(123)
data = pd.DataFrame({
         'LOAN':np.random.randint(17, 65, 50) * 100
})
loan = data["LOAN"]
s = pd.cut(loan, np.arange((loan.min()-100), (loan.max()+100), 500))
out = s.value_counts(normalize=True).sort_index()
print (out)
(1600, 2100]    0.155556
(2100, 2600]    0.066667
(2600, 3100]    0.066667
(3100, 3600]    0.088889
(3600, 4100]    0.088889
(4100, 4600]    0.088889
(4600, 5100]    0.244444
(5100, 5600]    0.111111
(5600, 6100]    0.088889
Name: LOAN, dtype: float64

s1 = loan.groupby(s).size()
print (s1)
LOAN
(1600, 2100]     7
(2100, 2600]     3
(2600, 3100]     3
(3100, 3600]     4
(3600, 4100]     4
(4100, 4600]     4
(4600, 5100]    11
(5100, 5600]     5
(5600, 6100]     4
Name: LOAN, dtype: int64
out = s1.div(s1.sum())
print (out)
LOAN
(1600, 2100]    0.155556
(2100, 2600]    0.066667
(2600, 3100]    0.066667
(3100, 3600]    0.088889
(3600, 4100]    0.088889
(4100, 4600]    0.088889
(4600, 5100]    0.244444
(5100, 5600]    0.111111
(5600, 6100]    0.088889
Name: LOAN, dtype: float64

相关内容

  • 没有找到相关文章

最新更新