5

This Question was resolved. There was an invisible space in "Env"("Env "). Below codes were correct.

Question :

I don't understand error messages about sort_values. Below is printed dataframe.

 print(df)
                                          Value1  Value2  Value3  Value4
     Function Type       Name                                           
     Env      condition   Board             BD#1    BD#2    BD#3    BD#4
                          Temp             High     Room    Low    High 
                          Volt              High     Low     Typ    High
                          SIDD               200      85     185     200
                          Freq             1600M   1600M   1600M   1600M
     CBT      status      CBT_CH0             00      00      00      00
              Input       Data0_CH0           0B      0A      0B      0A
                          Data1_CH0           0C      0C      0D      0B
                          Data2_CH0           07      08      09      06
                          Data3_CH0           06      05      04      08
                          Data4_CH0           01      01      01      03
                          Data5_CH0           00      01      01      02
              Output      Data0_CH0_out       0B      0A      0B      0A
                          Data1_CH0_out       0C      0C      0A      0B
                          Data2_CH0_out       07      04      05      06
                          Data3_CH0_out       06      08      09      05
                          Data4_CH0_out       01      03      04      02
                          Data5_CH0_out       00      02      03      01
              status      CBT_CH1             00      01      02      01
              Input       Data0_CH1           0B      0C      0D      0A
                          Data1_CH1           0C      0B      0A      0D
                          Data2_CH1           07      06      05      08
                          Data3_CH1           06      07      06      07
                          Data4_CH1           01      02      03      02
                          Data5_CH1           00      00      01      01
              Output      Data0_CH1_out       0B      0A      0B      0C
                          Data1_CH1_out       0C      0B      0C      0B
                          Data2_CH1_out       07      07      08      06
                          Data3_CH1_out       06      04      05      04
                          Data4_CH1_out       01      02      03      02
                          Data5_CH1_out       00      01      02      01

I used sort_values to sort column by index in alphabetical order.

  df= df.sort_values(by=('Env','condition','Temp'), axis=1)

But, error message is occurred.

I think the index list('Env','condition','Temp') was correct.

I don't understand below error messages and why that is occurred.

Please, let me know the meaning of below messages.

     ---------------------------------------------------------------------------
     KeyError                                  Traceback (most recent call last)
     <ipython-input-57-1a4fbe80928c> in <module>()
     ----> 1 df= df.sort_values(by=('Env','condition','Temp'), axis=1)

     C:\Users\13ZD970\Anaconda3\lib\site-packages\pandas\core\frame.py in sort_values(self, by, axis, ascending, inplace, kind, na_position)
        4419             by = by[0]
        4420             k = self._get_label_or_level_values(by, axis=axis,
     -> 4421                                                 stacklevel=stacklevel)
        4422 
        4423             if isinstance(ascending, (tuple, list)):

     C:\Users\13ZD970\Anaconda3\lib\site-packages\pandas\core\generic.py in _get_label_or_level_values(self, key, axis, stacklevel)
        1380             values = self.axes[axis].get_level_values(key)._values
        1381         else:
     -> 1382             raise KeyError(key)
        1383 
        1384         # Check for duplicates

     KeyError: ('Env', 'condition', 'Temp')
10
  • Try df= df.sort_values(by=['Env','condition','Temp']), note that your error stack trace looks different to your posted code. if you transpose the df then the columns become the index so you then need to pass axis=1 Commented Jan 29, 2019 at 8:42
  • Sorry, I posted wrong argument. even though I used df= df.sort_values(('Env','condition','Temp', axis=1)), above error messages are occured. Commented Jan 29, 2019 at 8:45
  • I modified it according to your advice. the contents of error messages was changed. Commented Jan 29, 2019 at 8:48
  • Actually don't you want df.sort_values(by=['Function', 'Type','Name'], axis=1) What you passed was the top level values in your multi-index Commented Jan 29, 2019 at 8:49
  • I changed bigger dataframe. new error messages was generated Commented Jan 29, 2019 at 9:03

1 Answer 1

2

The only thing that you've to change is:

df= df.sort_values(by=['Env','condition','Temp'], axis=1)

The by parameter takes either a string value or a list of string values. Not a tuple, (as you did).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.