Python 熊猫:can';不创建透视表

Python 熊猫:can';不创建透视表,python,pandas,Python,Pandas,这是一个数据帧: test_num file_num dose is_anneal test_name fail 0 10 0 0.00 False test1 1 10 1 10.42 False test1 2 10 2 34.34 False test1 3

这是一个数据帧:

    test_num  file_num    dose  is_anneal  test_name  fail
0         10         0    0.00      False    test1       
1         10         1   10.42      False    test1       
2         10         2   34.34      False    test1       
3         10         3   57.06      False    test1       
4         10         4  103.45      False    test1       
5         10         5  200.69      False    test1       
6         10         6  300.24      False    test1     8↑
7         11         0    0.00      False    test2       
8         11         1   10.42      False    test2       
9         11         2   34.34      False    test2       
10        11         3   57.06      False    test2     2↑
11        11         4  103.45      False    test2     2↑
12        11         5  200.69      False    test2     2↑
13        11         6  300.24      False    test2  2↑,8↑
我想制作一个透视表,以便
test\u num
test\u name
都可以显示:

fail_data_pivot = fd.pivot(columns='dose', index=['test_num', 'test_name'], values='fail')

>>ValueError: Wrong number of items passed 14, placement implies 2

fail_data_pivot = fd.pivot_table(columns='dose', index=['test_num', 'test_name'], values='fail')

>>pandas.core.base.DataError: No numeric types to aggregate
如果我只留下了
test\u num
,那么它可以工作:

fail_data_pivot = fd.pivot(columns='dose', index='test_num', values='fail')
print(fail_data_pivot)

dose     0.00   10.42  34.34  57.06  103.45 200.69 300.24
test_num                                                 
10                                                     8↑
11                                2↑     2↑     2↑  2↑,8↑
这正是我想要的:

dose                   0.00   10.42  34.34  57.06  103.45 200.69 300.24
test_num  test_name                                               
10        test1                                                     8↑
11        test2                                2↑     2↑     2↑  2↑,8↑

如何创建具有多个索引的透视表?

正如我所说,您可以使用
pivot\u表

df.pivot_table(columns='dose', index=['test_num', 'test_name'], values='fail',aggfunc='first')

您应该将pivot_表与aggfunc一起使用,而不是pivot@WeNYoBen我应该使用哪个聚合函数?aggfunc=','。join@WeNYoBen但价值观已经在改变joined@May我知道为什么您需要两个相同的键['test_num','test_name']?