10. Sort

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.

Hide code cell content
import pandas as pd
committee_list = pd.read_csv("https://raw.githubusercontent.com/california-civic-data-coalition/first-python-notebook/master/docs/src/_static/committees.csv")
contrib_list = pd.read_csv("https://raw.githubusercontent.com/california-civic-data-coalition/first-python-notebook/master/docs/src/_static/contributions.csv")
my_prop = 'PROPOSITION 064- MARIJUANA LEGALIZATION. INITIATIVE STATUTE.'
my_committees = committee_list[committee_list.prop_name == my_prop]
merged_everything = pd.merge(committee_list, contrib_list, on="calaccess_committee_id")
merged_prop = merged_everything[merged_everything.prop_name == my_prop]
support = merged_prop[merged_prop.committee_position == 'SUPPORT']
oppose = merged_prop[merged_prop.committee_position == 'OPPOSE']
merged_prop.sort_values("amount")
ocd_prop_id calaccess_prop_id ccdc_prop_id prop_name ccdc_committee_id calaccess_committee_id committee_name_x committee_position committee_name_y calaccess_filing_id date_received contributor_lastname contributor_firstname contributor_city contributor_state contributor_zip contributor_employer contributor_occupation contributor_is_self_employed amount
83297 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 458 1376077 CALIFORNIANS FOR SENSIBLE REFORM, SPONSORED BY... SUPPORT CALIFORNIANS FOR SENSIBLE REFORM, SPONSORED BY... 2082735 2016-08-04 GHOST MANAGEMENT GROUP, LLC DBA WEEDMAPS NaN IRVINE CA 92618 NaN NaN False -150000.0
83830 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 461 1343793 CALIFORNIANS FOR RESPONSIBLE MARIJUANA REFORM,... SUPPORT CALIFORNIANS FOR RESPONSIBLE MARIJUANA REFORM,... 2130986 2016-11-02 ROBINETT PETER SAN FRANCISCO CA 94114 APPLE, INC. PROGRAMMER False -900.0
83764 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 461 1343793 CALIFORNIANS FOR RESPONSIBLE MARIJUANA REFORM,... SUPPORT CALIFORNIANS FOR RESPONSIBLE MARIJUANA REFORM,... 2083494 2016-07-13 WOOD DAVID CALGARY AB T2P0R3 BORDEN LADNER GERVAIS ATTORNEY False -100.0
83735 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 461 1343793 CALIFORNIANS FOR RESPONSIBLE MARIJUANA REFORM,... SUPPORT CALIFORNIANS FOR RESPONSIBLE MARIJUANA REFORM,... 2083494 2016-07-27 SAMIMI-ARDESTAN AMIRGHASEM NORTH VANCOUVER BC V7J1V2 MEDIGREEN CHIEF EXECUTIVE OFFICER False -100.0
83843 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 461 1343793 CALIFORNIANS FOR RESPONSIBLE MARIJUANA REFORM,... SUPPORT CALIFORNIANS FOR RESPONSIBLE MARIJUANA REFORM,... 2130986 2016-10-26 MEDLOCK MARSTON ALHAMBRA CA 91801 NaN RETIRED False -100.0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2106 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 465 1385745 FUND FOR POLICY REFORM (NONPROFIT 501(C)(4)) SUPPORT FUND FOR POLICY REFORM (NONPROFIT 501(C)(4)) 2096507 2016-09-27 FUND FOR POLICY REFORM NaN WILMINGTON DE 19809 NaN NaN False 1403000.0
2105 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 465 1385745 FUND FOR POLICY REFORM (NONPROFIT 501(C)(4)) SUPPORT FUND FOR POLICY REFORM (NONPROFIT 501(C)(4)) 2082414 2016-09-15 FUND FOR POLICY REFORM NaN WILMINGTON DE 19809 NaN NaN False 1970000.0
83883 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 464 1385506 DRUG POLICY ACTION - NON PROFIT 501C4, YES ON ... SUPPORT DRUG POLICY ACTION - NON PROFIT 501C4, YES ON ... 2083608 2016-09-15 FUND FOR POLICY REFORM (NONPROFIT 501 (C)(4)) NaN NEW YORK NY 10019 NaN NaN False 1970000.0
83116 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 456 1381808 YES ON 64, CALIFORNIANS TO CONTROL, REGULATE A... SUPPORT YES ON 64, CALIFORNIANS TO CONTROL, REGULATE A... 2083528 2016-09-15 DRUG POLICY ACTION - NON PROFIT 501C4, YES ON ... NaN SACRAMENTO CA 95815 NaN NaN False 2000000.0
83179 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 456 1381808 YES ON 64, CALIFORNIANS TO CONTROL, REGULATE A... SUPPORT YES ON 64, CALIFORNIANS TO CONTROL, REGULATE A... 2083528 2016-09-15 PARKER AND AFFILIATED ENTITIES SEAN PALO ALTO CA 94301 NaN NaN False 4000000.0

860 rows × 20 columns

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_prop.sort_values("amount", ascending=False)
ocd_prop_id calaccess_prop_id ccdc_prop_id prop_name ccdc_committee_id calaccess_committee_id committee_name_x committee_position committee_name_y calaccess_filing_id date_received contributor_lastname contributor_firstname contributor_city contributor_state contributor_zip contributor_employer contributor_occupation contributor_is_self_employed amount
83179 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 456 1381808 YES ON 64, CALIFORNIANS TO CONTROL, REGULATE A... SUPPORT YES ON 64, CALIFORNIANS TO CONTROL, REGULATE A... 2083528 2016-09-15 PARKER AND AFFILIATED ENTITIES SEAN PALO ALTO CA 94301 NaN NaN False 4000000.0
83116 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 456 1381808 YES ON 64, CALIFORNIANS TO CONTROL, REGULATE A... SUPPORT YES ON 64, CALIFORNIANS TO CONTROL, REGULATE A... 2083528 2016-09-15 DRUG POLICY ACTION - NON PROFIT 501C4, YES ON ... NaN SACRAMENTO CA 95815 NaN NaN False 2000000.0
2105 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 465 1385745 FUND FOR POLICY REFORM (NONPROFIT 501(C)(4)) SUPPORT FUND FOR POLICY REFORM (NONPROFIT 501(C)(4)) 2082414 2016-09-15 FUND FOR POLICY REFORM NaN WILMINGTON DE 19809 NaN NaN False 1970000.0
83883 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 464 1385506 DRUG POLICY ACTION - NON PROFIT 501C4, YES ON ... SUPPORT DRUG POLICY ACTION - NON PROFIT 501C4, YES ON ... 2083608 2016-09-15 FUND FOR POLICY REFORM (NONPROFIT 501 (C)(4)) NaN NEW YORK NY 10019 NaN NaN False 1970000.0
2106 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 465 1385745 FUND FOR POLICY REFORM (NONPROFIT 501(C)(4)) SUPPORT FUND FOR POLICY REFORM (NONPROFIT 501(C)(4)) 2096507 2016-09-27 FUND FOR POLICY REFORM NaN WILMINGTON DE 19809 NaN NaN False 1403000.0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
83843 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 461 1343793 CALIFORNIANS FOR RESPONSIBLE MARIJUANA REFORM,... SUPPORT CALIFORNIANS FOR RESPONSIBLE MARIJUANA REFORM,... 2130986 2016-10-26 MEDLOCK MARSTON ALHAMBRA CA 91801 NaN RETIRED False -100.0
83735 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 461 1343793 CALIFORNIANS FOR RESPONSIBLE MARIJUANA REFORM,... SUPPORT CALIFORNIANS FOR RESPONSIBLE MARIJUANA REFORM,... 2083494 2016-07-27 SAMIMI-ARDESTAN AMIRGHASEM NORTH VANCOUVER BC V7J1V2 MEDIGREEN CHIEF EXECUTIVE OFFICER False -100.0
83764 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 461 1343793 CALIFORNIANS FOR RESPONSIBLE MARIJUANA REFORM,... SUPPORT CALIFORNIANS FOR RESPONSIBLE MARIJUANA REFORM,... 2083494 2016-07-13 WOOD DAVID CALGARY AB T2P0R3 BORDEN LADNER GERVAIS ATTORNEY False -100.0
83830 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 461 1343793 CALIFORNIANS FOR RESPONSIBLE MARIJUANA REFORM,... SUPPORT CALIFORNIANS FOR RESPONSIBLE MARIJUANA REFORM,... 2130986 2016-11-02 ROBINETT PETER SAN FRANCISCO CA 94114 APPLE, INC. PROGRAMMER False -900.0
83297 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 458 1376077 CALIFORNIANS FOR SENSIBLE REFORM, SPONSORED BY... SUPPORT CALIFORNIANS FOR SENSIBLE REFORM, SPONSORED BY... 2082735 2016-08-04 GHOST MANAGEMENT GROUP, LLC DBA WEEDMAPS NaN IRVINE CA 92618 NaN NaN False -150000.0

860 rows × 20 columns

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

merged_prop.sort_values("amount", ascending=False).head()
ocd_prop_id calaccess_prop_id ccdc_prop_id prop_name ccdc_committee_id calaccess_committee_id committee_name_x committee_position committee_name_y calaccess_filing_id date_received contributor_lastname contributor_firstname contributor_city contributor_state contributor_zip contributor_employer contributor_occupation contributor_is_self_employed amount
83179 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 456 1381808 YES ON 64, CALIFORNIANS TO CONTROL, REGULATE A... SUPPORT YES ON 64, CALIFORNIANS TO CONTROL, REGULATE A... 2083528 2016-09-15 PARKER AND AFFILIATED ENTITIES SEAN PALO ALTO CA 94301 NaN NaN False 4000000.0
83116 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 456 1381808 YES ON 64, CALIFORNIANS TO CONTROL, REGULATE A... SUPPORT YES ON 64, CALIFORNIANS TO CONTROL, REGULATE A... 2083528 2016-09-15 DRUG POLICY ACTION - NON PROFIT 501C4, YES ON ... NaN SACRAMENTO CA 95815 NaN NaN False 2000000.0
2105 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 465 1385745 FUND FOR POLICY REFORM (NONPROFIT 501(C)(4)) SUPPORT FUND FOR POLICY REFORM (NONPROFIT 501(C)(4)) 2082414 2016-09-15 FUND FOR POLICY REFORM NaN WILMINGTON DE 19809 NaN NaN False 1970000.0
83883 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 464 1385506 DRUG POLICY ACTION - NON PROFIT 501C4, YES ON ... SUPPORT DRUG POLICY ACTION - NON PROFIT 501C4, YES ON ... 2083608 2016-09-15 FUND FOR POLICY REFORM (NONPROFIT 501 (C)(4)) NaN NEW YORK NY 10019 NaN NaN False 1970000.0
2106 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 465 1385745 FUND FOR POLICY REFORM (NONPROFIT 501(C)(4)) SUPPORT FUND FOR POLICY REFORM (NONPROFIT 501(C)(4)) 2096507 2016-09-27 FUND FOR POLICY REFORM NaN WILMINGTON DE 19809 NaN NaN False 1403000.0

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

support.sort_values("amount", ascending=False).head()
ocd_prop_id calaccess_prop_id ccdc_prop_id prop_name ccdc_committee_id calaccess_committee_id committee_name_x committee_position committee_name_y calaccess_filing_id date_received contributor_lastname contributor_firstname contributor_city contributor_state contributor_zip contributor_employer contributor_occupation contributor_is_self_employed amount
83179 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 456 1381808 YES ON 64, CALIFORNIANS TO CONTROL, REGULATE A... SUPPORT YES ON 64, CALIFORNIANS TO CONTROL, REGULATE A... 2083528 2016-09-15 PARKER AND AFFILIATED ENTITIES SEAN PALO ALTO CA 94301 NaN NaN False 4000000.0
83116 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 456 1381808 YES ON 64, CALIFORNIANS TO CONTROL, REGULATE A... SUPPORT YES ON 64, CALIFORNIANS TO CONTROL, REGULATE A... 2083528 2016-09-15 DRUG POLICY ACTION - NON PROFIT 501C4, YES ON ... NaN SACRAMENTO CA 95815 NaN NaN False 2000000.0
2105 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 465 1385745 FUND FOR POLICY REFORM (NONPROFIT 501(C)(4)) SUPPORT FUND FOR POLICY REFORM (NONPROFIT 501(C)(4)) 2082414 2016-09-15 FUND FOR POLICY REFORM NaN WILMINGTON DE 19809 NaN NaN False 1970000.0
83883 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 464 1385506 DRUG POLICY ACTION - NON PROFIT 501C4, YES ON ... SUPPORT DRUG POLICY ACTION - NON PROFIT 501C4, YES ON ... 2083608 2016-09-15 FUND FOR POLICY REFORM (NONPROFIT 501 (C)(4)) NaN NEW YORK NY 10019 NaN NaN False 1970000.0
2106 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 465 1385745 FUND FOR POLICY REFORM (NONPROFIT 501(C)(4)) SUPPORT FUND FOR POLICY REFORM (NONPROFIT 501(C)(4)) 2096507 2016-09-27 FUND FOR POLICY REFORM NaN WILMINGTON DE 19809 NaN NaN False 1403000.0

And now how about the opposition with the oppose DataFrame.

oppose.sort_values("amount", ascending=False).head()
ocd_prop_id calaccess_prop_id ccdc_prop_id prop_name ccdc_committee_id calaccess_committee_id committee_name_x committee_position committee_name_y calaccess_filing_id date_received contributor_lastname contributor_firstname contributor_city contributor_state contributor_zip contributor_employer contributor_occupation contributor_is_self_employed amount
83876 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 463 1387789 SAM ACTION, INC., A COMMITTEE AGAINST PROPOSIT... OPPOSE SAM ACTION, INC., A COMMITTEE AGAINST PROPOSIT... 2083142 2016-07-20 SCHAUER JULIE OAKS PA 19456 NaN RETIRED False 1000000.0
83349 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 459 1382568 PUBLIC AND MENTAL HEALTH ADVOCATES AGAINST 64,... OPPOSE PUBLIC AND MENTAL HEALTH ADVOCATES AGAINST 64,... 2098048 2016-09-27 SAM ACTION, INC. NaN ALEXANDRIA VA 22314 NaN NaN False 400000.0
83347 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 459 1382568 PUBLIC AND MENTAL HEALTH ADVOCATES AGAINST 64,... OPPOSE PUBLIC AND MENTAL HEALTH ADVOCATES AGAINST 64,... 2083071 2016-09-15 SAM ACTION, INC. NaN ALEXANDRIA VA 22314 NaN NaN False 225000.0
83345 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 459 1382568 PUBLIC AND MENTAL HEALTH ADVOCATES AGAINST 64,... OPPOSE PUBLIC AND MENTAL HEALTH ADVOCATES AGAINST 64,... 2083071 2016-09-12 SAM ACTION, INC. NaN ALEXANDRIA VA 22314 NaN NaN False 200000.0
83877 ocd-contest/232cb72c-1b11-4293-b4e2-a181e80d172e 1381868 83 PROPOSITION 064- MARIJUANA LEGALIZATION. INITI... 463 1387789 SAM ACTION, INC., A COMMITTEE AGAINST PROPOSIT... OPPOSE SAM ACTION, INC., A COMMITTEE AGAINST PROPOSIT... 2083142 2016-07-25 SCHAUER JULIE OAKS CA 19456 NaN RETIRED False 150000.0