我对编程很陌生,而且我正在学习,我一直试着做了大约一天而没有运气。我有一个非常大的数据集,它目前显示如下:
服务器1政策1 full2 1086
server 1 policy 1 diff2 100.5
服务器3政策5 full3 500
服务器1策略3 app 10
服务器2政策2 full1 75
server 2 policy 2 diff1 62
我需要它像这样显示
|客户|政策名称|计划名称|卷
|服务器1 |政策1 | full2 | 1086
| policy 1 | diff2 | 100.5
| policy 3 | app | 10
|总| 1196.5
|服务器2政策2 | full2 | 75
| policy 2 | diff1 | 65
|总计| 137
我尝试了很多不同的方法,分组似乎没有工作,它抛出了一堆错误。除了volume字段之外的所有内容的值都有一个分配给它的对象值。 volume字段定义为float64。 就像我说的那样,我对此非常陌生,所以如果我不是100%理解这些概念,请耐心等待。
答案 0 :(得分:0)
此代码将按客户端
计算总计import pandas as pd
# |Client |policy name |schedule name |volume
# server 1 policy 1 full2 1086
# server 1 policy 1 diff2 100.5
# server 3 policy 5 full3 500
# server 1 policy 3 app 10
# server 2 policy 2 full1 75
# server 2 policy 2 diff1 62
df = pd.DataFrame({'Client': ['server 1', 'server 1', 'server 3', 'server 1', 'server 2', 'server 2'],
'policy name': ['policy 1', 'policy 1', 'policy 5', 'policy 3', 'policy 2', 'policy 2'],
'schedule name': ['full2', 'diff2', 'full3', 'app', 'full1', 'diff1'],
'volume': [1086, 100.5, 500, 10, 75, 62]})
def print_with_and_total(df):
print(df.groupby(['Client', 'policy name', 'schedule name']).sum())
print('Total: {}'.format(df['volume'].sum()))
for x in df.groupby('Client'):
print_with_and_total(x[1])
# volume
# Client policy name schedule name
# server 1 policy 1 diff2 100.5
# full2 1086.0
# policy 3 app 10.0
# Total: 1196.5
# volume
# Client policy name schedule name
# server 2 policy 2 diff1 62.0
# full1 75.0
# Total: 137.0
# volume
# Client policy name schedule name
# server 3 policy 5 full3 500.0
# Total: 500.0