Skip to content
Advertisement

pivot table raise error uniquely valued index error

I am trying to modify the following dataset in python 3/pandas

   Rank    Maj  Rank   Maj  Rank    Maj  Rank    Maj  Rank     Maj  Rank     Maj
0  2.00  31.92  3.00  0.00  4.00  33.72  5.00  24.89  6.00  0.00.1  7.00  148.35
1     8  28.26     9     0    10   5.96    11   7.66    12       0    13    6.19
2    14   5.63    15     0    16  17.43    17  26.73    18       0    19    84.7
3    20  25.98    21     0    22   8.65    23   6.38    24       0    25    3.98
4    26   2.44    27     0    28   3.43    29   2.75    30       0    31     1.8
5    32   1.46    33     0    34   1.79    35   2.49    36       0    37    2.51
6    38   1.85    39     0    40   1.48    41   1.05    42       0    43    0.56
7    44   0.36    45     0    46   0.31    47    0.2    49    0.32    50     0.2

into a dataframe that will have the first columns or index to be the rank and the second column all the Maj value. Something like that:

   Rank    Maj 
   2.00  31.92  
      8  28.26    
     14   5.63    
     20  25.98  
     26   2.44   
     32   1.46   
     38   1.85  
     44   0.36 
     3.00  0.00
     9     0   
     15     0    
     21     0    
     27     0    
     33     0   
     39     0  
     45     0  

     13    6.19
     19    84.7
     25    3.98
     31     1.8
     37    2.51
     43    0.56
     50     0.2

I am trying to do that with a table pivot:

table.pivot_table(index = "Rank", columns = "Maj")

But get the following error:

Traceback (most recent call last):
  File "ReadReport.py", line 42, in <module>
    table.pivot_table(index = "Rank", columns = "Maj")
  File "C:Python38-32libsite-packagespandascoreframe.py", line 6070, in pivot_table
    return pivot_table(
  File "C:Python38-32libsite-packagespandascorereshapepivot.py", line 95, in pivot_table
    values = values.drop(key)
  File "C:Python38-32libsite-packagespandascoreindexesbase.py", line 5013, in drop
    indexer = self.get_indexer(labels)
  File "C:Python38-32libsite-packagespandascoreindexesbase.py", line 2733, in get_indexer
    raise InvalidIndexError(
pandas.core.indexes.base.InvalidIndexError: Reindexing only valid with uniquely valued Index objects

But i do not have any duplicated value in the Rank. It goes from 2 to 50.

My main goal is to print Rank over Maj.

Thanks for your help

Advertisement

Answer

You can use np.reshape:

print (pd.DataFrame(df.to_numpy().reshape((-1, 2)), columns=["Rank", "Maj"]))

   Rank     Maj
0     2   31.92
1     3       0
2     4   33.72
3     5   24.89
4     6  0.00.1
5     7  148.35
6     8   28.26
7     9       0
8    10    5.96
9    11    7.66

...
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement