Python Pandas Pivot-将Aggc列值更改为行

时间:2018-10-14 06:10:35

标签: python-3.x pandas

df = pd.DataFrame({"AgentName": ['airlines_all_flightdetails', 'airlines_all_flightdetails', 'airlines_all_flightdetails', 'airlines_all_flightdetails', 'airlines_all_flightdetails', 'airlines_all_flightdetails', 'airlines_all_flightdetails', 'airlines_all_flightdetails', 'airlines_all_flightdetails', 'airlines_all_flightdetails'],
               "OutputTableName": ['carrier=xx', 'carrier=xx', 'carrier=xx', 'carrier=xx', 'carrier=xx', 'carrier=xx', 'carrier=xx', 'carrier=xx', 'carrier=xx', 'carrier=xx'],
               "LastModified": ['10/13/2018', '10/13/2018', '10/12/2018', '10/12/2018', '10/11/2018', '10/11/2018', '10/10/2018', '10/09/2018', '10/08/2018', '10/07/2018'],
               "Size": [540722, 1995963, 541308, 2179389, 469444, 1519627, 472355, 472622, 1691951, 2012013]
                })
pivot = df.pivot_table(index=['AgentName', 'OutputTableName'], values='Size', columns='LastModified', aggfunc=[np.sum, 'count'])

#stacked = pivot.stack(0)

print (pivot.head())

我必须求和大小以及每个日期有多少条记录。

完成枢纽操作后,“求和和计数”显示在列中,而我想要这样的行

what code I have to write to achieve same kind of output.

谢谢。

1 个答案:

答案 0 :(得分:0)

如果您只取消注释stacked = pivot.stack(0)

,您的代码应返回您想要的内容

如果您希望索引列像图像一样排序并使用名称Values,则可以执行以下操作:

# Given
df = pd.DataFrame({"AgentName": ['airlines_all_flightdetails', 'airlines_all_flightdetails', 'airlines_all_flightdetails', 'airlines_all_flightdetails', 'airlines_all_flightdetails', 'airlines_all_flightdetails', 'airlines_all_flightdetails', 'airlines_all_flightdetails', 'airlines_all_flightdetails', 'airlines_all_flightdetails'],
               "OutputTableName": ['carrier=xx', 'carrier=xx', 'carrier=xx', 'carrier=xx', 'carrier=xx', 'carrier=xx', 'carrier=xx', 'carrier=xx', 'carrier=xx', 'carrier=xx'],
               "LastModified": ['10/13/2018', '10/13/2018', '10/12/2018', '10/12/2018', '10/11/2018', '10/11/2018', '10/10/2018', '10/09/2018', '10/08/2018', '10/07/2018'],
               "Size": [540722, 1995963, 541308, 2179389, 469444, 1519627, 472355, 472622, 1691951, 2012013]
                })

# Given
pivot = df.pivot_table(index=['AgentName', 'OutputTableName'], values='Size', columns='LastModified', aggfunc=[np.sum, 'count'])

# Moves 'Values' from columns to index
stacked = pivot.stack(0)

# sorts index to relfect image
stacked.sort_index(level = 2, ascending = False, inplace = True)

# renames inner index to 'Values'
stacked.index.rename('Values', level = 2, inplace = True)

# rename inner index values
stacked.rename(index = {
    'sum' : 'Sum of Size',
    'count' : 'Count of OutputFileName'
})

应返回以下内容:

output