pandas中的多值或多索引数据透视表

时间:2018-06-09 16:34:02

标签: python pandas numpy

我有一个像

这样的样本数据表
import pandas as pd

compnaies = ['Microsoft', 'Google', 'Amazon', 'Microsoft', 'Facebook', 'Google']
products = ['OS', 'Search', 'E-comm', 'E-comm', 'Social Media', 'OS']
count = [5,7,3,19,23,54]
average = [1.2,3.4,2.4,5.2,3.2,4.4]

df = pd.DataFrame({'company' : compnaies, 'product':products,
                   'count':count , 'average' : average})
df

    average company count   product
0   1.2 Microsoft   5   OS
1   3.4 Google      7   Search
2   2.4 Amazon      3   E-comm
3   5.2 Microsoft   19  E-comm
4   3.2 Facebook    23  Social Media
5   4.4 Google      54  OS

现在我想在平均值'上创建数据透视图。和'计算'但是我无法定义这两个值,这里的示例代码只有一个'平均值'

df.pivot_table(index='company', columns='product', values='average', fill_value=0)

输出将是

enter image description here

但是我需要以下格式的数据,有人可以请求帮助同时我尝试了堆栈,并创建多索引数据框但是它没有提供所需的输出,我会在需要时共享代码

我需要在excel中下载所需的输出

enter image description here

1 个答案:

答案 0 :(得分:1)

set_indexstackunstack一起使用:

df = (df.set_index(['company','product'])
       .stack()
       .unstack(axis=1)
       .rename_axis([None, None])
       .rename_axis(None, axis=1))
print (df)
                   E-comm    OS  Search  Social Media
Amazon    count       3.0   NaN     NaN           NaN
          average     2.4   NaN     NaN           NaN
Facebook  count       NaN   NaN     NaN          23.0
          average     NaN   NaN     NaN           3.2
Google    count       NaN  54.0     7.0           NaN
          average     NaN   4.4     3.4           NaN
Microsoft count      19.0   5.0     NaN           NaN
          average     5.2   1.2     NaN           NaN