"TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed" while sorting pandas dataframe index

38,087

Solution 1

Is that what you want?

In [14]: h
Out[14]:
                 alpha1  alpha2    gamma1  gamma2       chi2min gender  age
filename
F35_HC_532d.dat  0.0000   0.000       NaN    0.00  1.000000e+25      F   35
M48_HC_551d.dat  0.7353   3.943  0.425922    0.15  2.072617e+01      M   48
M24_HC_458d.dat  0.7777   4.754  0.463753    0.15  1.390893e+01      M   24
M48_HC_552d.dat  0.7633   3.672  0.394370    0.15  1.965052e+01      M   48
M40_HC_506d.dat  0.7793   3.271  0.513597    0.20  1.089716e+01      M   40

In [15]: h.sort_values('age')
Out[15]:
                 alpha1  alpha2    gamma1  gamma2       chi2min gender  age
filename
M24_HC_458d.dat  0.7777   4.754  0.463753    0.15  1.390893e+01      M   24
F35_HC_532d.dat  0.0000   0.000       NaN    0.00  1.000000e+25      F   35
M40_HC_506d.dat  0.7793   3.271  0.513597    0.20  1.089716e+01      M   40
M48_HC_551d.dat  0.7353   3.943  0.425922    0.15  2.072617e+01      M   48
M48_HC_552d.dat  0.7633   3.672  0.394370    0.15  1.965052e+01      M   48

Solution 2

I think your index is filename. Maybe you could try something like:

h['index1'] = h.index
h.sort_values(by=['index1', 'age'])

But also it will not make so much sense since it will not change the order. Alternatively you can try:

h.sort_values(by='age')

Then:

h.reindex([range(some_number)])
Share:
38,087
Peaceful
Author by

Peaceful

Updated on March 28, 2020

Comments

  • Peaceful
    Peaceful about 4 years

    I have a following dataframe h:

    In [24]: h.head()
    Out[24]: 
                     alpha1  alpha2    gamma1  gamma2       chi2min gender  age
    filename                                                                   
    F35_HC_532d.dat  0.0000   0.000       NaN    0.00  1.000000e+25      F   35
    M48_HC_551d.dat  0.7353   3.943  0.425922    0.15  2.072617e+01      M   48
    M24_HC_458d.dat  0.7777   4.754  0.463753    0.15  1.390893e+01      M   24
    M48_HC_552d.dat  0.7633   3.672  0.394370    0.15  1.965052e+01      M   48
    M40_HC_506d.dat  0.7793   3.271  0.513597    0.20  1.089716e+01      M   40
    

    I am trying to sort the dataframe index according to age values:

    In [25]: h.sort_index(h.sort_values('age'))
    

    This throws an error:

    TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed
    

    What am I missing? Any ideas?

    • MaxU - stop genocide of UA
      MaxU - stop genocide of UA about 7 years
      Are you after: h.sort_values('age')? Can you provide a desired data set?
    • Peaceful
      Peaceful about 7 years
      @MaxU : I think I didn't get you. I am trying to arrange the rows of the dataframe according to the age values.
  • Peaceful
    Peaceful about 7 years
    That's another method. I want to know what is wrong with my method.
  • Peaceful
    Peaceful about 7 years
    I realized now. That's right. I was making a silly mistake.
  • Cenk_Mitir
    Cenk_Mitir about 7 years
    the problem in your method is indices are filenames not numbers