gpt4 book ai didi

python - 如何使用 np.arange 按系列类型划分系列

转载 作者:行者123 更新时间:2023-12-01 06:26:59 27 4
gpt4 key购买 nike

我想做我上传的 png 插图所具有的功能,主要有两列,其中有数字。带星号的代码为我提供了类和“SquareMiles”。百分比栏更难获得。我将系列号数组除以该列的总系列数,但没有成功。这个部门如何才能完成?

Total = ca_land.get("SquareMiles")
Total1 = Total.sum()

percent = np.arange(Total/Total1)

* ca_classess = ca_land.groupby('Class').sum()

ca_classes = ca_classess.assign(
Percentage = percent

)

enter image description here

示例数据:

{'Class': {52: 'Forest & Woodland',   126: 'Forest & Woodland',   128: 'Forest & Woodland',   131: 'Forest & Woodland',   133: 'Forest & Woodland'},  'EcologicalSystem': {52: 'East Gulf Coastal Plain Near-Coast Pine Flatwoods - Open Understory Modifier',   126: 'California Central Valley Mixed Oak Savanna',   128: 'California Coastal Closed-Cone Conifer Forest and Woodland',   131: 'California Coastal Live Oak Woodland and Savanna',   133: 'California Lower Montane Blue Oak-Foothill Pine Woodland and Savanna'},  'SquareMiles': {52: 0.03266424261286,   126: 4350.262402786333,   128:
85.94309723386156, 131: 1726.1481557315549, 133: 6587.099312156705}}

最佳答案

我认为聚合sum后有必要划分SquareMiles列:

d = {'Class': {52: 'Forest & Woodland1', 126: 'Forest & Woodland1', 128: 'Forest & Woodland1', 131: 'Forest & Woodland', 133: 'Forest & Woodland'}, 'EcologicalSystem': {52: 'East Gulf Coastal Plain Near-Coast Pine Flatwoods - Open Understory Modifier', 126: 'California Central Valley Mixed Oak Savanna', 128: 'California Coastal Closed-Cone Conifer Forest and Woodland', 131: 'California Coastal Live Oak Woodland and Savanna', 133: 'California Lower Montane Blue Oak-Foothill Pine Woodland and Savanna'}, 'SquareMiles': {52: 0.03266424261286, 126: 4350.262402786333, 128: 85.94309723386156, 131: 1726.1481557315549, 133: 6587.099312156705}}
ca_land = pd.DataFrame(d)
<小时/>
Total1 = ca_land['SquareMiles'].sum()
ca_classes = (ca_land.groupby('Class').sum()
.assign(Percentage=lambda x: x['SquareMiles'] / Total1))
print (ca_classes)
SquareMiles Percentage
Class
Forest & Woodland 8313.247468 0.652046
Forest & Woodland1 4436.238164 0.347954

或者没有分配的解决方案可读性更好:

ca_classes = ca_land.groupby('Class').sum()
ca_classes['Percentage'] = ca_classes['SquareMiles'] / ca_land['SquareMiles'].sum()
print (ca_classes)
SquareMiles Percentage
Class
Forest & Woodland 8313.247468 0.652046
Forest & Woodland1 4436.238164 0.347954

关于python - 如何使用 np.arange 按系列类型划分系列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60086876/

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