Use Python to Display Data on Political Donations

Python can be used to quickly process a csv file full of data and display in a chart that will likely make more sense to the viewer. This post will show how you can obtain data about a company’s political donations and use Python to create a chart showing if there is a pattern based on the committees where Senators receive donations from the company.

  1. Go to Open Secrets and search for you company of choice. I am using Exxon Mobil, and a quick search immediately leads me to this page which compiles political data related to the company. There should be a similar page for your company of choice.
  2. Click on where it says “Congressional Committees” as seen below:
This image has an empty alt attribute; its file name is image-1.png

3) From here, scroll down to where it says “Senate Totals” and click on the button to the right that says “export to csv.” The page defaults to the latest cycle that it has access to data but you can choose any year or House Totals as well, for this exercise I am just using the Senate.

4) We are interested in two fields within the table, “Committee” and “Total.” However the data in the Total column is formatted as “currency,” which will interfere with our Python script. So click on the “C” above that column and click on it so it highlights to entire column. Next, up and to the right, in the “Number” section of the header there is a drop-down menu that will say “General” or “Currency.” Click on the menu and click on “General,” even if it already said “General.” Hopefully the dollar signs in the column have disappeared.

5) Save the file as a csv file, with the name “Senate_Totals,” and save it to whichever folder will contain the Python script you are about to use.

6) The Python script below will identify the index number for each header, and at this point it is also good to use the script to make sure the csv is properly formatted and located.

import csv

filename = 'Senate_Totals.csv'
with open(filename) as f:
	reader = csv.reader(f)
    header_row = next(reader)

	for index, column_header in enumerate(header_row):
		print(index, column_header)

7) You should receive a list that looks like this

8) This confirms that the Committee column is indexed number 1 and Total is number 2.

9) Finally, we open a new Python file and save it to the same folder as the csv. Now you can copy and paste into your file the script below and run it.

import csv
from plotly.graph_objs import Bar, Layout
from plotly import offline

filename = 'data/Senate_Totals.csv'
with open(filename) as f:
	reader = csv.reader(f)
	header_row = next(reader)
	
	# get committees and total temperatures from this file
	committees, totals = [], []
	for row in reader:
		committee = (row[1])
		total = int(row[2])
		committees.append(committee)
		totals.append(total)

#visualize results
x_values = committees
data = [Bar(x=x_values, y=totals)]

x_axis_config = {'title': 'Committee Name'}
y_axis_config = {'title': 'Total Donated'}
my_layout = Layout(title='Total Donations to Senate Committees', xaxis=x_axis_config, yaxis=y_axis_config)
offline.plot({'data': data, 'layout': my_layout}, filename='d6.html')

This should produce a table that looks like the one below. Also note that if you move your cursor over one of the bars a sign will appear and list the actual dollar amount donated.

This table helps to identify a company’s political priorities based on how much it contributes to Senators in different committees. It is best to consider this evidence, but not proof of political priorities.

Compare Contributions to Lobbying Activity

You can strengthen your argument that the trends in this table represent political priorities by comparing these results against the company’s lobbying efforts. When a company uses lobbyists, they must submit filings that officially register which bills they are lobbying.

One can look into the bills lobbied by the company and see which committees voted on those bills. It also makes sense to look at how much the company spent on lobbying those bills. Note that sometimes the paperwork does not perfectly divide the company’s expenses along the lines of individual pieces of legislation.

You can find these filings at Open Secrets, for example click here to see a summary of Exxon Mobil’s lobbying. Or you can go to the source for the information. The House and Senate each have their own public databases for lobbying records. Click here for the House database and click here for the Senate database.

A forthcoming post on lobbying records will provide further information on this topic.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s