Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Pivot Table manually sort columns [duplicate]

For a given data frame:

UUT                  testa  testb  testc  testd
DateTime                                
2017-11-21 18:47:29    1.0    1.0    1.0    3.0
2017-11-21 18:47:30    1.0    2.0    1.0    4.0
2017-11-21 18:47:31    1.0    2.0    5.0    2.0
2017-11-21 18:47:32    1.0    2.0    5.0    1.0
2017-11-21 18:47:33    1.0    2.0    5.0    4.0
2017-11-21 18:47:34    1.0    2.0    5.0    1.0

how can I manually rearrange the columns anyway I want it for example if I want to the following sequence:

testc, testd, testa, testb

So the table and the plot will be in this way:

UUT                  testc  testd  testa  testb  
DateTime                                         
2017-11-21 18:47:29    1.0    3.0    1.0    1.0  
2017-11-21 18:47:30    1.0    4.0    1.0    2.0  
2017-11-21 18:47:31    5.0    2.0    1.0    2.0  
2017-11-21 18:47:32    5.0    1.0    1.0    2.0  
2017-11-21 18:47:33    5.0    4.0    1.0    2.0  
2017-11-21 18:47:34    5.0    1.0    1.0    2.0
like image 565
user97662 Avatar asked Dec 19 '17 08:12

user97662


People also ask

How do I manually sort columns in pivot table?

In a PivotTable, click the small arrow next to Row Labels and Column Labels cells. Click a field in the row or column you want to sort. on Row Labels or Column Labels, and then click the sort option you want. To sort data in ascending or descending order, click Sort A to Z or Sort Z to A.

Can you sort multiple columns in a pivot table?

If you go to Data -> Sort , you can add multiple levels of sorting, i.e. "I want to sort by revenue, then total quantity, then alphabetic Product order, etc." When in pivot table sorting, even if you only have one Row column (say Product), you cannot sort by multiple columns in values.

How do I sort values based on multiple columns in Pandas?

You can sort pandas DataFrame by one or multiple (one or more) columns using sort_values() method and by ascending or descending order. To specify the order, you have to use ascending boolean property; False for descending and True for ascending. By default, it is set to True.


1 Answers

You can use:

df = df.reindex_axis(['testc','testd', 'testa','testb'], axis=1)
like image 84
Joe Avatar answered Sep 25 '22 23:09

Joe