This (article] [http://pbpython.com/simple-graphing-pandas.html] will walk through how to start doing some simple graphing in pandas. I am using a new data file that is the same format as my previous article but includes data for only 20 customers. First we are going to import pandas, numpy and matplot lib. I am also showing the versions I'm testing so you can make sure yours is compatible.
In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
pd.__version__
Out[1]:
Next, enable iPython to display matplotlib graphs. As an alternative you can run ipython notebook.
In [2]:
%matplotlib inline
We will read in the file like we did in the previous article but I'm going to tell it to treat the date column as a date field so I can do some re-sampling later.
In [3]:
sales=pd.read_csv("sample-salesv2.csv",parse_dates=['date'])
sales.head()
Out[3]:
Now that we have read in the data, we can do some quick analysis
In [4]:
sales.describe()
Out[4]:
We can actually learn some pretty helpful info from this simple command: For example, we can tell that customers on average purchases 10.3 items per transaction and that the average cost of the transaction was $579.84. It is also easy to see the min and max so you understand the range of the data.
In [5]:
sales['unit price'].describe()
Out[5]:
It is easy to call describe on a single column too. I can see that my average price is \$56.18 but it ranges from \$10.06 to \$99.97.
I am showing the output of dtypes so that you can see that the date column is a datetime field. I also scan this to make sure that any columns that have numbers are floats or ints so that I can do additional analysis in the future.
In [6]:
sales.dtypes
Out[6]:
Now we remove some columns to make additional analysis easier.
In [7]:
customers = sales[['name','ext price','date']]
customers.head()
Out[7]:
This representation has multiple lines for each customer. In order to understand purchasing patterns, let's group all the customers by name.
In [8]:
customer_group = customers.groupby('name')
customer_group.size()
Out[8]:
Now that our data is in a simple format to manipulate, let's determine how much each customer purchased during our time frame.
The sum function allows us to quickly sum up all the values by customer. We can also sort the data using the sort command.
In [9]:
sales_totals = customer_group.sum()
sales_totals.sort_values(by=['ext price']).head()
Out[9]:
Now that we know what the data look like, tt is very simple to create a quick bar chart plot.
In [10]:
my_plot = sales_totals.plot(kind='bar')
Unfortunately this chart is a little ugly. With a few tweaks we can make it a little more impactful. Let's try:
In [11]:
my_plot = sales_totals.sort_values(by=['ext price'],ascending=False).plot(kind='bar',legend=None,title="Total Sales by Customer")
my_plot.set_xlabel("Customers")
my_plot.set_ylabel("Sales ($)")
Out[11]:
This actually tells us a little about our biggest customers and how much difference there is between their sales and our smallest customers.
Now, let's try to see how the sales break down by category.
In [12]:
customers = sales[['name','category','ext price','date']]
customers.head()
Out[12]:
We can use groupby to organize the data by category and name.
In [13]:
category_group=customers.groupby(['name','category']).sum()
category_group.head()
Out[13]:
The category representation looks good but we need to break it apart to graph it as a stacked bar graph. Unstack can do this for us.
In [14]:
category_group.unstack().head()
Out[14]:
Now plot it.
In [15]:
my_plot = category_group.unstack().plot(kind='bar',stacked=True,title="Total Sales by Customer")
my_plot.set_xlabel("Customers")
my_plot.set_ylabel("Sales")
Out[15]:
Now clean some of this up a little bit. We can specify the figure size and customize the legend.
In [16]:
my_plot = category_group.unstack().plot(kind='bar',stacked=True,title="Total Sales by Customer",figsize=(9, 7))
my_plot.set_xlabel("Customers")
my_plot.set_ylabel("Sales")
my_plot.legend(["Total","Belts","Shirts","Shoes"], loc=9,ncol=4)
Out[16]:
Now that we know who the biggest customers are and how they purchase products, we might want to look at purchase patterns in more detail.
Let's take another look at the data and try to see how large the individual purchases are. A histogram allows us to group purchases together so we can see how big the customer transactions are.
In [17]:
purchase_patterns = sales[['ext price','date']]
purchase_patterns.head()
Out[17]:
In [18]:
purchase_plot = purchase_patterns['ext price'].hist(bins=20)
purchase_plot.set_title("Purchase Patterns")
purchase_plot.set_xlabel("Order Amount($)")
purchase_plot.set_ylabel("Number of orders")
Out[18]:
After looking at this group
We can look at purchase patterns over time. We can see that most of our transactions are less than $500 and only a very few are about $1500.
Another interesting way to look at the data would be by sales over time. Do we have certain months where we are busier than others?
Let's get the data down to order size and date.
In [19]:
purchase_patterns = sales[['ext price','date']]
purchase_patterns.head()
Out[19]:
If we want to analyze the data by date, we need to set the date column as the index.
In [20]:
purchase_patterns = purchase_patterns.set_index('date')
purchase_patterns.head()
Out[20]:
One of the really cool things that pandas allows us to do is resample the data. If we want to look at the data by month, we can easily resample and sum it all up.
purchase_patterns.resample('M',how=sum)
Plotting the data is now very easy
In [21]:
purchase_plot = purchase_patterns.resample('M').sum().plot(title="Total Sales by Month",legend=None)
December is our peak month and April is the slowest.
Let's say we really like this plot and want to save it somewhere for a presentation.
In [22]:
fig = purchase_plot.get_figure()
fig.savefig("total-sales.png")
In [ ]: