Chapter 10: Hello sortingΒΆ

Another simple but common technique for analyzing data is sorting.

What were the ten biggest contributions? We can find the answer by using the sort_values method to rearrange our list using the amount field.

merged.sort_values("amount")

Note that returns the DataFrame resorted in ascending order from lowest to highest. That is pandas default way of sorting.

To answer our question you’ll need to reverse that, so that values are sorted in descending order from biggest to smallest. It’s a little tricky at first, but here’s how to do it with sort_values.

merged.sort_values("amount", ascending=False)

You can limit the result to the top five by chaining the head method at the end.

merged.sort_values("amount", ascending=False).head()

We can now use the new variable to rank the five biggest supporting contributions by using sort_values again.

support.sort_values("amount", ascending=False).head()

And now how about the opposition.

oppose.sort_values("amount", ascending=False).head()