Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas pivot_table, sort values by columns

I am a new user to Pandas and I love it!

I am trying to create a pivot table in Pandas. Once I have pivot table the way I want, I would like to rank the values by the columns.

I've attached an image from Excel as it is easier to see in tabular format what I am trying to achieve. Link to image

I've searched through stackoverflow but am having trouble finding an answer. I tried using .sort() but this doesn't work. Any help will be appreciated.

Thanks in advance

like image 522
Kah Avatar asked Dec 13 '16 11:12

Kah


People also ask

How do I sort a Pandas DataFrame by column values?

To sort the DataFrame based on the values in a single column, you'll use . sort_values() . By default, this will return a new DataFrame sorted in ascending order. It does not modify the original DataFrame.

Can you sort by multiple columns 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.

How do I sort a pivot table by a specific column?

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.

How do I sort a column in descending order in Pandas?

By setting ascending=False , we can sort in descending order also.


1 Answers

This should do what you are looking for:

In [1]: df = pd.DataFrame.from_dict([{'Country': 'A', 'Year':2012, 'Value': 20, 'Volume': 1}, {'Country': 'B', 'Year':2012, 'Value': 100, 'Volume': 2}, {'Country': 'C', 'Year':2013, 'Value': 40, 'Volume': 4}])

In [2]: df_pivot = pd.pivot_table(df, index=['Country'], columns = ['Year'],values=['Value'], fill_value=0)

In [3]: df_pivot
Out [4]:
    Value     
Year     2012 2013
Country           
A          20    0
B         100    0
C           0   40

In [5]: df = df_pivot.reindex(df_pivot['Value'].sort_values(by=2012, ascending=False).index)

Out [6]: 
    Value     
Year     2012 2013
Country           
B         100    0
A          20    0
C           0   40

Basically it gets the index of the sorted values and reindex the initial pivot table.

like image 96
Algold Avatar answered Oct 08 '22 21:10

Algold