gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 08:21:05 26 4
gpt4 key购买 nike

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

Python 代码:

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

有没有一种方法可以用来显示百分比而不是计数?我应该对新的 DataFrame 执行第二次操作吗?如果是这样,我该怎么办?

最佳答案

使用value_counts使用标准化:

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

关于python - 如何将 pandas.series.groupby() 表从计数转换为百分比频率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54644711/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com