Chapter 10: Using Python Data Analysis with Practical Examples
Now we are going to look at a quick example of how we can complete a bit of the data analysis that we want to do with Python. We are going to complete some of our work with the help of the Pandas library that we talked about before to help us get this one done. Keep in mind that this is just a quick example, there are many other parts that can come into play, and it is possible that the data analysis you want to complete is going to be more complex.
Make sure to open up the notebook that you would like to use for this. You can stick with the traditional Python environment to get things done, or you can choose to work with the IPython as we chose before. The first thing that we need to do with this is to make sure that we import the right libraries. Just because they are on your computer does not mean the code knows that you want to work with them. Instead, you need to bring them in so that the code knows you mean to use them .
For this project, we are going to work with two main libraries, the panda's library, and the NumPy library. The code that we can use to make these come out and work for us include:
import pandas as pd
import numpy as np
When that is done, we can go through and read some of the sample data that will help us to create our analysis. We also want to be able to get a good summary of how this is going to look. Some of the codings that we need to use here include the following:
 
SALES=pd.read_csv("sample-sales.csv")
SALES.head()
Take some time to go through this and run the compiler so that you can see what is going to show up, and how the information is going to look. You should get a nice table that has all of the necessary data to make this easier .
When this is all done, we are able to go on to the next step. We want to use what is known as the function of a pivot table. This is going to be used to help us summarize the sales and then will turn the rows of data into something that we are able to use. Since this is our first project, we are going to keep it as simple as possible so work with the code below:
report = SALES.pivot_table(values=['quantity'],index=['Account Name'],columns=['category'], aggfunc=np.sum)
report.head(n=10)
When we do this, we are going to be able to do a few more things. This particular command is going to show us the number of products that all of our customers have been able to purchase, and it all shows up in just that one command.
While this is something that is impressive, you may notice when you look at the output that there are a number of NaN’s in the output. This is going to stand for Not a Number and will show us where there is not a value in place for us to work with. In many cases, we want to change this so that it says something like 0 instead of the NaN. We can do this with the function of fill_value as we see in the code below:
 
report = SALES.pivot_table(values=['quantity'],index=['Account Name'],columns=['category'], fill_value=0, aggfunc=np.sum)
report.head(n=10)
When you check out the output on this one, it should be a little bit nicer and cleaner to look at. We are going to then take this to one more step before we finish up. This will help us to see some more of the power that will show up with the pivot_table. For this one, we are going to work with some coding that will show us how much ins ales we were able to do:
report = SALES.pivot_table(values=['ext price','quantity'],index=['Account Name'],columns=['category'], fill_value=0,aggfunc=np.sum)
report.head(n=10)
It is even possible for us to take all of this and output it to Excel. We do need to go through a few steps to make this happen, such as converting all of the information back to our DataFrame. Then it can be written out to work in Excel. The code that we can use for this one is below:
report.to_excel ('report.xlsx', sheet_name='Sheet1')
That is as simple as this process is. We can utilize some of the codes that are present with the Python language in order to get them to behave in the right manner, and you will find that it makes things a whole lot easier to do in the end. You can add in more parts and make it more complicated as well, but overall, these are the basics of what we can do when it comes to working with this process.