我有这个数据框
DistrictName RegionName Value
Ashburton Canterbury 451
Auckland City Auckland 2459
Banks Peninsula Canterbury 132
Buller West Coast 361
Carterton Wellington 75
Central Hawkes Bay Hawkes Bay 67
Central Otago* Central Otago & Lakes District Central Otago & Lakes District 190
Christchurch City Canterbury 2046
Clutha Otago 119
Dunedin City Otago 312
我正在尝试将此作为重点,并为每个区域提供值&;地区,这么做
pivot_table(districtleveldatav1.head(10), values=['Value'],index=['RegionName'],
columns=['DistrictName'], aggfunc=np.sum, margins=True).stack('DistrictName').drop('All', level=0)
给我这个
我需要做两件事
我怎么能在每个区域的底部都有'All'in DistrictName。?可能带有标签“Total”
是否可以根据我的自定义订单手动订购RegionName。类似的地区名称?谢谢
这些列似乎是按名字的字母顺序排列的
All
首先出现,因为它以A开头,按字母顺序排列时,前面没有其他列要使名称
Total
而不是All
,可以使用margins_name
参数,如中所示在设置
margins=True
之后这会给你
参见documentation
相关问题 更多 >
编程相关推荐