<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 First Python Notebook
Step1:  There. You've just written your first Python code. You've entered two integers (the 2's) and added them together using the plus sign operator. Not so bad, right?
Next, let's introduce one of the basics of computer programming, a variable.
Variables are like containers that hold different types of data so you can go back and refer to them later. They’re fundamental to programming in any language, and you’ll use them all the time when you're writing Python.
Move down to the next box. Now let's put that number two into our first variable.
Step2:  In this case, we’ve created a variable called san and assigned it the integer value 2.
In Python, variable assignment is done with the = sign. On the left is the name of the variable you want to create (it can be anything) and on the right is the value that you want to assign to that variable.
If we use the print command on the variable, Python will output its contents to the terminal because that value is stored in the variable. Let's try it.
Step3:  We can do the same thing again with a different variable name
Step4:  Then add those two together the same way we added the numbers at the top.
Step5:  Variables can contain many different kinds of data types. There are integers, strings, floating point numbers (decimals), lists and dictionaries.
Step6:  Playing with data we invent can be fun, but it's a long way from investigative journalism. 
Now's the time for us to get our hands on some real data and get some real work done.
Your assignment
Step7:  Print that variable and you see that open has created a file "object" that offers a number of different ways to interact with the contents of the file.
Step8:  One thing a file object can do is read in all of the data from the file. Let's do that next and store the contents in a new variable.
Step9:  That's all good, but the data is printing out as one big long string. If we're going to do some real analysis, we need Python to recognize and respect the structure of our data, in the way an Excel spreadsheet would.
To do that, we're going to need something smarter than open. We're going to need something like pandas.
Act 3
Step10:  Opening our CSV isn't any harder than with open, you just need to know the right trick to make it work.
Step11:  Great. Now let's do it again and assign it to a variable this time
Step12:  Now let's see what that returns when we print it.
Step13:  Here's how you can see the first few rows
Step14:  How many rows are there? Here's how to find out.
Step15:  Even with that simple question and answer, we've begun the process of interviewing our data.
In some ways, your database is no different from a human source. Getting a good story requires careful, thorough questioning.
In the next section we will move ahead by conducting an interview with pandas to pursue our quest of finding out the biggest donors to Proposition 64.
Act 4
Step16:  We've got it sorted the wrong way. Let's reverse it.
Step17:  Now let's limit it to the top 10.
Step18:  What is the total sum of contributions that have been reported?
First, let's get our hands on the column with our numbers in it. In pandas you can do that like so.
Step19:  Now adding it up is this easy.
Step20:  There's our big total. Why is it lower than the ones I quoted above? That's because campaigns are only required to report the names of donors over $200, so our data is missing all of the donors who gave smaller amounts of money.
The overall totals are reported elsewhere in lump sums and cannot be replicated by adding up the individual contributions. Understanding this is crucial to understanding not just this data, but all campaign finance data, which typically has this limitation.
Filtering
Adding up a big total is all well and good. But we're aiming for something more nuanced. We want to separate the money for the proposition from the money against it. To do that, we'll need to learn how to filter.
First let's look at the column we're going to filter by
Step21:  Now let's filter using that column using pandas oddball method
Step22:  Stick that in a variable
Step23:  So now we can ask
Step24:  Next
Step25:  Now let's ask the same questions of the opposing side.
Step26:  How about the sum total of contributions for each?
Step27:  Grouping
One thing we noticed as we explored the data is that there are a lot of different committees. A natural question follows
Step28:  Wow. That's pretty ugly. Why? Because pandas is weird. 
To convert a raw dump like that into a clean table (known in pandas slang as a "dataframe") you can add the reset_index method to the end of your code. Don't ask why.
Step29:  Now let's sort it by size
Step30:  Okay. Committees are good. But what about something a little more interesting. Who has given the most money?
To do that, we'll group by the two name columns at the same time.
Step31:  But which side where they are? Add in the position column to see that too.
Step32:  Pretty cool, right? Now now that we've got this interesting list of people, let's see if we can make a chart out of it.
Act 5
Step33:  Before we'll get started, let's run one more trick to configure matplotlib to show its charts in our notebook.
Step34:  Now let's save the data we want to chart into a variable
Step35:  Making a quick bar chart is as easy as this.
Step36:  It's really those first five that are the most interesting, so let's trim our chart.
Step37:  What are those y axis labels? Those are the row number (pandas calls them indexes) of each row. We don't want that. We want the names.
Step38:  Okay, but what if I want to combine the first and last name?
First, make a new column. First let's look at what we have now.
Step39:  In plain old Python, we created a string at the start of our less. Remember this?
Step40:  Combining strings can be as easy as addition.
Step41:  And if we want to get a space in there yet we can do something like
Step42:  And guess what we can do the same thing with two columns in our table, and use a pandas trick that will apply it to every row.
Step43:  Now let's see the results
Step44:  Now let's chart that.
Step45:  That's all well and good, but this chart is pretty ugly. If you wanted to hand this data off to your graphics department, or try your hand at a simple chart yourself using something like Chartbuilder, you'd need to export this data into a spreadsheet. 
It's this easy.
<ASSISTANT_TASK:>
Python Code:
2+2
Explanation: First Python Notebook: Scripting your way to the story
By Ben Welsh
A step-by-step guide to analyzing data with Python and the Jupyter Notebook.
This tutorial will teach you how to use computer programming tools to analyze data by exploring contributors to campaigns for and again Proposition 64, a ballot measure asking California voters to decide if recreational marijuana should be legalized.
This guide was developed by Ben Welsh for a Oct. 2, 2016, "watchdog workshop" organized by Investigative Reporters and Editors at San Diego State University's school of journalism. The class is designed for beginners who have zero Python experience.
Prelude: Prequisites
Before you can begin, your computer needs the following tools installed and working to participate.
A command-line interface to interact with your computer
Version 2.7 of the Python programming language
The pip package manager and virtualenv environment manager for Python
Command-line interface
Unless something is wrong with your computer, there should be a way to open a window that lets you type in commands. Different operating systems give this tool slightly different names, but they all have some form of it, and there are alternative programs you can install as well.
On Windows you can find the command-line interface by opening the "command prompt." Here are instructions for Windows 10 and for Windows 8 and earlier versions. On Apple computers, you open the "Terminal" application. Ubuntu Linux comes with a program of the same name.
Python
If you are using Mac OSX or a common flavor of Linux, Python version 2.7 is probably already installed and you can test to see what version, if any, is already available by typing the following into your terminal.
bash
python -V
Even if you find it already on your machine, Mac users should install it separately by following these instructions offered by The Hitchhikers Guide to Python. 
Windows people can find a similar guide here which will have them try downloading and installing Python from here.
pip and virtualenv
The pip package manager makes it easy to install open-source libraries that expand what you're able to do with Python. Later, we will use it to install everything needed to create a working web application.
If you don't have it already, you can get pip by following these instructions. In Windows, it's necessary to make sure that the Python Scripts directory is available on your system's PATH so it can be called from anywhere on the command line. This screencast can help.
Verify pip is installed with the following.
bash
pip -V
The virtualenv environment manager makes it possible to create an isolated corner of your computer where all the different tools you use to build an application are sealed off.
It might not be obvious why you need this, but it quickly becomes important when you need to juggle different tools
for different projects on one computer. By developing your applications inside separate virtualenv environments, you can use different versions of the same third-party Python libraries without a conflict. You can also more easily recreate your project on another machine, handy when you want to copy your code to a server that publishes pages on the Internet.
You can check if virtualenv is installed with the following.
bash
virtualenv --version
If you don't have it, install it with pip.
```bash
pip install virtualenv
If you're on a Mac or Linux and get an error saying you lack permissions, try again as a superuser.
sudo pip install virtualenv
```
If that doesn't work, try following this advice.
Act 1: Hello Jupyter Notebook
Start by creating a new development environment with virtualenv in your terminal. Name it after our application.
bash
virtualenv first-python-notebook
Jump into the directory it created.
bash
cd first-python-notebook
Turn on the new virtualenv, which will instruct your terminal to only use those libraries installed
inside its sealed space. You only need to create the virtualenv once, but you'll need to repeat these
"activation" steps each time you return to working on this project.
```bash
In Linux or Mac OSX try this...
. bin/activate
In Windows it might take something more like...
cd Scripts
activate
cd ..
```
Use pip on the command line to install Jupyter Notebook, an open-source tool for writing and sharing Python scripts.
bash
pip install jupyter
Start up the notebook from your terminal.
bash
jupyter notebook
That will open up a new tab in your default web browser that looks something like this:
Click the "New" button in the upper right and create a new Python 2 notebook. Now you're all setup and ready to start writing code.
Act 2: Hello Python
You are now ready to roll within the Jupyter Notebook's framework for writing Python. Don't stress. There's nothing too fancy about it. You can start by just doing a little simple math. Type the following into the first box, then hit the play button in the toolbox (or hit SHIFT+ENTER on your keyboard).
End of explanation
san = 2
Explanation: There. You've just written your first Python code. You've entered two integers (the 2's) and added them together using the plus sign operator. Not so bad, right?
Next, let's introduce one of the basics of computer programming, a variable.
Variables are like containers that hold different types of data so you can go back and refer to them later. They’re fundamental to programming in any language, and you’ll use them all the time when you're writing Python.
Move down to the next box. Now let's put that number two into our first variable.
End of explanation
print san
Explanation: In this case, we’ve created a variable called san and assigned it the integer value 2.
In Python, variable assignment is done with the = sign. On the left is the name of the variable you want to create (it can be anything) and on the right is the value that you want to assign to that variable.
If we use the print command on the variable, Python will output its contents to the terminal because that value is stored in the variable. Let's try it.
End of explanation
diego = 2
Explanation: We can do the same thing again with a different variable name
End of explanation
san + diego
Explanation: Then add those two together the same way we added the numbers at the top.
End of explanation
string = "Hello"
decimal = 1.2
list_of_strings = ["a", "b", "c", "d"]
list_of_integers = [1, 2, 3, 4]
list_of_whatever = ["a", 2, "c", 4]
my_phonebook = {'Mom': '713-555-5555', 'Chinese Takeout': '573-555-5555'}
Explanation: Variables can contain many different kinds of data types. There are integers, strings, floating point numbers (decimals), lists and dictionaries.
End of explanation
data_file = open("./first-python-notebook.csv", "r")
Explanation: Playing with data we invent can be fun, but it's a long way from investigative journalism. 
Now's the time for us to get our hands on some real data and get some real work done.
Your assignment: Proposition 64.
The use and sale of marijuana for recreational purposes is illegal in California. Proposition 64, scheduled to appear on the November 2016 ballot, asked voters if it ought to be legalized. A "yes" vote would support legalization. A "no" vote would oppose it. A similar measure, Proposition 19, was defeated in 2010.
According to California's Secretary of State, more than $16 million was been raised to campaign in support of Prop. 64 as of September 20. Just over 2 million was been raised to oppose it.
Your mission, should you choose to accept it, is to download a list of campaign contributors and figure out the biggest donors both for and against the measure.
Click here to download the file as a list of comma-separated values. This is known as a CSV file. It is the most common way you will find data published online. Save the file with the name first-python-notebook.csv in the same directory where you made this notebook. 
Python can read files using the built-in open function. You feed two things into it: 1) The path to the file; 2) What type of operation you'd like it to execute on the file. "r" stands for read.
End of explanation
print data_file
Explanation: Print that variable and you see that open has created a file "object" that offers a number of different ways to interact with the contents of the file.
End of explanation
data = data_file.read()
print data
Explanation: One thing a file object can do is read in all of the data from the file. Let's do that next and store the contents in a new variable.
End of explanation
import pandas
Explanation: That's all good, but the data is printing out as one big long string. If we're going to do some real analysis, we need Python to recognize and respect the structure of our data, in the way an Excel spreadsheet would.
To do that, we're going to need something smarter than open. We're going to need something like pandas.
Act 3: Hello pandas
Lucky for us, Python already has tools filled with functions to do pretty much anything you’d ever want to do with a programming language: navigate the web, parse data, interact with a database, run fancy statistics, build a pretty website and so much more.
Some of those tools are included a toolbox that comes with the language, known as the standard library. Others have been built by members of Python's developer community and need to be downloaded and installed from the web.
For this exercise, we're going to install and use pandas, a tool developed by a financial investment firm that has become the leading open-source tool for accessing and analyzing data.
There are several others we could use instead (like agate) but we're picking pandas here because it's the most popular and powerful.
We'll install pandas the same way we installed the Jupyter Notebook earlier: Our friend pip. Save your notebook, switch to your window/command prompt and hit CTRL-C. That will kill your notebook and return you to the command line. There we'll install pandas.
bash
pip install pandas
Now let's restart our notebook and get back to work.
bash
jupyter notebook
Use the next open box to import pandas into our script, so we can use all its fancy methods here in our script.
End of explanation
pandas.read_csv("./first-python-notebook.csv")
Explanation: Opening our CSV isn't any harder than with open, you just need to know the right trick to make it work.
End of explanation
table = pandas.read_csv("./first-python-notebook.csv")
Explanation: Great. Now let's do it again and assign it to a variable this time
End of explanation
table.info()
Explanation: Now let's see what that returns when we print it.
End of explanation
table.head()
Explanation: Here's how you can see the first few rows
End of explanation
print len(table)
Explanation: How many rows are there? Here's how to find out.
End of explanation
table.sort_values("AMOUNT")
Explanation: Even with that simple question and answer, we've begun the process of interviewing our data.
In some ways, your database is no different from a human source. Getting a good story requires careful, thorough questioning.
In the next section we will move ahead by conducting an interview with pandas to pursue our quest of finding out the biggest donors to Proposition 64.
Act 4: Hello analysis
Let's start with something easy. What are the ten biggest contributions?
That will require a sort using the column with the money in it.
End of explanation
table.sort_values("AMOUNT", ascending=False)
Explanation: We've got it sorted the wrong way. Let's reverse it.
End of explanation
table.sort_values("AMOUNT", ascending=False).head(10)
Explanation: Now let's limit it to the top 10.
End of explanation
table['AMOUNT']
Explanation: What is the total sum of contributions that have been reported?
First, let's get our hands on the column with our numbers in it. In pandas you can do that like so.
End of explanation
table['AMOUNT'].sum()
Explanation: Now adding it up is this easy.
End of explanation
table['COMMITTEE_POSITION']
Explanation: There's our big total. Why is it lower than the ones I quoted above? That's because campaigns are only required to report the names of donors over $200, so our data is missing all of the donors who gave smaller amounts of money.
The overall totals are reported elsewhere in lump sums and cannot be replicated by adding up the individual contributions. Understanding this is crucial to understanding not just this data, but all campaign finance data, which typically has this limitation.
Filtering
Adding up a big total is all well and good. But we're aiming for something more nuanced. We want to separate the money for the proposition from the money against it. To do that, we'll need to learn how to filter.
First let's look at the column we're going to filter by
End of explanation
table[table['COMMITTEE_POSITION'] == 'SUPPORT']
Explanation: Now let's filter using that column using pandas oddball method
End of explanation
support_table = table[table['COMMITTEE_POSITION'] == 'SUPPORT']
Explanation: Stick that in a variable
End of explanation
print len(support_table)
Explanation: So now we can ask: How many contributions does the supporting side have?
End of explanation
support_table.sort_values("AMOUNT", ascending=False).head(10)
Explanation: Next: What are the 10 biggest supporting contributions?
End of explanation
oppose_table = table[table['COMMITTEE_POSITION'] == 'OPPOSE']
print len(oppose_table)
oppose_table.sort_values("AMOUNT", ascending=False).head(10)
Explanation: Now let's ask the same questions of the opposing side.
End of explanation
support_table['AMOUNT'].sum()
oppose_table['AMOUNT'].sum()
Explanation: How about the sum total of contributions for each?
End of explanation
table.groupby("COMMITTEE_NAME")['AMOUNT'].sum()
Explanation: Grouping
One thing we noticed as we explored the data is that there are a lot of different committees. A natural question follows: Which ones have raised the most money?
To figure that out, we'll need to group the data by that column and sum up the amount column for each. Here's how pandas does that.
End of explanation
table.groupby("COMMITTEE_NAME")['AMOUNT'].sum().reset_index()
Explanation: Wow. That's pretty ugly. Why? Because pandas is weird. 
To convert a raw dump like that into a clean table (known in pandas slang as a "dataframe") you can add the reset_index method to the end of your code. Don't ask why.
End of explanation
table.groupby("COMMITTEE_NAME")['AMOUNT'].sum().reset_index().sort_values("AMOUNT", ascending=False)
Explanation: Now let's sort it by size
End of explanation
table.groupby(["FIRST_NAME", "LAST_NAME"])['AMOUNT'].sum().reset_index().sort_values("AMOUNT", ascending=False)
Explanation: Okay. Committees are good. But what about something a little more interesting. Who has given the most money?
To do that, we'll group by the two name columns at the same time.
End of explanation
table.groupby([
    "FIRST_NAME",
    "LAST_NAME",
    "COMMITTEE_POSITION"
])['AMOUNT'].sum().reset_index().sort_values("AMOUNT", ascending=False)
Explanation: But which side where they are? Add in the position column to see that too.
End of explanation
import matplotlib.pyplot as plt
Explanation: Pretty cool, right? Now now that we've got this interesting list of people, let's see if we can make a chart out of it.
Act 5: Hello viz
Python has a number of charting tools that can work hand in hand with pandas. The most popular is matplotlib. It isn't the prettiest thing in the world, but it offers some reasonably straightfoward tools for making quick charts. And, best of all, it can display right here in our Jupyter Notebook.
Before we start, we'll need to make sure matplotlib is installed. Return to your terminal and try installing it with our buddy pip, as we installed other things before.
bash
    pip install matplotlib
Once you've got it in here, you can import it just as we would anything else. Though by adding the optional as option at the end we can create a shorter alias for accessing its tools.
End of explanation
%matplotlib inline
Explanation: Before we'll get started, let's run one more trick to configure matplotlib to show its charts in our notebook.
End of explanation
top_supporters = support_table.groupby(
    ["FIRST_NAME", "LAST_NAME"]
)['AMOUNT'].sum().reset_index().sort_values("AMOUNT", ascending=False).head(10)
top_supporters
Explanation: Now let's save the data we want to chart into a variable
End of explanation
top_supporters['AMOUNT'].plot.bar()
top_supporters['AMOUNT'].plot.barh()
Explanation: Making a quick bar chart is as easy as this.
End of explanation
top_supporters.head(5)['AMOUNT'].plot.barh()
Explanation: It's really those first five that are the most interesting, so let's trim our chart.
End of explanation
chart = top_supporters.head(5)['AMOUNT'].plot.barh()
chart.set_yticklabels(top_supporters['LAST_NAME'])
Explanation: What are those y axis labels? Those are the row number (pandas calls them indexes) of each row. We don't want that. We want the names.
End of explanation
top_supporters.head(5)
Explanation: Okay, but what if I want to combine the first and last name?
First, make a new column. First let's look at what we have now.
End of explanation
print string
Explanation: In plain old Python, we created a string at the start of our less. Remember this?
End of explanation
print string + "World"
Explanation: Combining strings can be as easy as addition.
End of explanation
print string + " " + "World"
Explanation: And if we want to get a space in there yet we can do something like:
End of explanation
top_supporters['FULL_NAME'] = top_supporters['FIRST_NAME'] + " " + top_supporters['LAST_NAME']
Explanation: And guess what we can do the same thing with two columns in our table, and use a pandas trick that will apply it to every row.
End of explanation
top_supporters.head()
Explanation: Now let's see the results
End of explanation
chart = top_supporters.head(5)['AMOUNT'].plot.barh()
chart.set_yticklabels(top_supporters['FULL_NAME'])
Explanation: Now let's chart that.
End of explanation
top_supporters.head(5).to_csv("top_supporters.csv")
Explanation: That's all well and good, but this chart is pretty ugly. If you wanted to hand this data off to your graphics department, or try your hand at a simple chart yourself using something like Chartbuilder, you'd need to export this data into a spreadsheet. 
It's this easy.
End of explanation
<END_TASK>