0

Here is a dataframe:

    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↑

I want to make a pivot table so that both test_num and test_name could present:

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

If I left only test_num then it works:

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↑

This is what I want instead:

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↑

How to make a pivot table with multiple index?

Michael
  • 5,095
  • 2
  • 13
  • 35

1 Answers1

1

As I said you can using pivot_table

df.pivot_table(columns='dose', index=['test_num', 'test_name'], values='fail',aggfunc='first')
BENY
  • 317,841
  • 20
  • 164
  • 234