[英]Is there a way to select the upper end of a bin using pandas.cut() function?
我對 pandas 中的垃圾箱有疑問。到目前為止,我的代碼如下所示:
africa_uhc = pd.cut(africa[("Universal health coverage (UHC) service coverage index")]/100, [0, 0.25, 0.50, 0.75, 1])
打印出來
29 (0.5, 0.75]
36 (0.25, 0.5]
111 (0.5, 0.75]
112 (0.5, 0.75]
118 (0.25, 0.5]
140 (0.25, 0.5]
141 (0.5, 0.75]
142 (0, 0.25]
Name: Universal health coverage (UHC) service coverage index, dtype: category
Categories (4, object): [(0, 0.25] < (0.25, 0.5] < (0.5, 0.75] < (0.75, 1]]
我想把第二行的數字拿出來做更多的聚合,有沒有辦法做到這一點? 或者有沒有辦法通過四舍五入將它們分成垃圾箱? 例如,索引 29 的值為 0.75 而不是 (0.5, 0.75)。謝謝!
您可以使用labels
參數來控制返回的內容。
import pandas as pd
df = pd.DataFrame({'UHC': [60,30,60,70,40,50,70,10]})
bins = [0, 0.25, 0.50, 0.75, 1]
print(pd.cut(df.UHC/100, bins))
#0 (0.5, 0.75]
#1 (0.25, 0.5]
#2 (0.5, 0.75]
#3 (0.5, 0.75]
#4 (0.25, 0.5]
#5 (0.25, 0.5]
#6 (0.5, 0.75]
#7 (0.0, 0.25]
#Name: UHC, dtype: category
#Categories (4, interval[float64]): [(0.0, 0.25] < (0.25, 0.5] < (0.5, 0.75] < (0.75, 1.0]]
print(pd.cut(df.UHC/100, bins, labels=bins[1:]))
#0 0.75
#1 0.50
#2 0.75
#3 0.75
#4 0.50
#5 0.50
#6 0.75
#7 0.25
#Name: UHC, dtype: category
#Categories (4, float64): [0.25 < 0.50 < 0.75 < 1.00]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.