Understand Data Analysis & Visualisation using Time series data. | by Shamim Ahmed | Towards Data Science

Humanity is blessed to have AI and Data Science because it helps us solve day to day problems. And quite often we encounter time series data in abundance, which has a strong resemblance with our daily activities.
In this post, we will learn how to get insights into data using data analysis and understand how data is behaving from time to time.
We used “Junglee Rummy business” time series dataset, download here. The dataset is owned by “Junglee Games”. Visit the link to get an overview of the dataset. You can also play the game.
We must get acknowledged to the terms associated with the dataset. Let’s have a look:-
Entry Fee: This is the Buy-in (money user pays) in rupees to enter the game.
Seat: Max number of players that can sit on the table i.e. 2,6 for the data set.
Composition: Actual number of players that actually joined the table.
Date: It’s a data set of 1st July 2018 to 30 Sep 2018 which gives daily data for each table configuration.
Configuration: Defined as the combination of Entry Fees — Seats — Composition.
Cut %: %age amount deducted for each game from each user.
‘#Users’: Distinct count of players (unique players) who played at least 1 game for table configuration for the date.
User Cash Game Count: Total number of games played by users on table configuration for the date. If user A, B, C play together a single game, then the value will be 3.
Rake: Total amount generated in revenue from a table configuration for the date.
Wager: Total amount paid by the users in terms of Entry Fees to play the game.
Let’s get started!
We would load important libraries and let's have a peek of the data.
import pandas as pdimport seaborn as snsimport matplotlib.pyplot as pltimport numpy as np# reading the data using pandasdf = pd.read_excel('BI Skill Test - Data Set.xlsx')df.head()
The dataset is quite intuitive. If it's not understandable, please read the column descriptions above. We would multiply the Cut% by 100 to make it computationally simple.
# Multiplying cut% with 100 to make it easy for computationdf['Cut %'] = df['Cut %'] * 100
Now that we had a brief overlook of the data. It’s very essential to analyze the basic statistics of the dataset.
# Basic statistics of tha datapd.options.display.float_format = '{:.0f}'.formatdf.describe()
In Data Science those stats play a vital role to get insights into the data. Few of the observations after getting through the numbers in the above table are:
Entry Fee: Average entry fee paid by users to play a game is Rs.854, the minimum entry fee is Rs 5 and the maximum entry fee is Rs 10000. Most of the time users pay Rs 100 as an entry fee to play a game.Seat: Average number of players sit together is 5, out of them, 4 play on average. Whereas 6 number of players play at max.Cut%: Average cut % is 13%. Most of the players pay 15% as a cut.#Users: Average number of users played at least one game per day is 165 whereas the maximum number of users played at least one game per day is 1155.Rake: Average amount of money, company earns per day is Rs 10137. The maximum amount of money, the company earned in a day is Rs 271200. Most of the time the company earns Rs 3195.Wager: Maximum amount players invested to play the game is Rs 4520000, whereas most of the time users pay Rs 24000.
Entry Fee: Average entry fee paid by users to play a game is Rs.854, the minimum entry fee is Rs 5 and the maximum entry fee is Rs 10000. Most of the time users pay Rs 100 as an entry fee to play a game.
Seat: Average number of players sit together is 5, out of them, 4 play on average. Whereas 6 number of players play at max.
Cut%: Average cut % is 13%. Most of the players pay 15% as a cut.
#Users: Average number of users played at least one game per day is 165 whereas the maximum number of users played at least one game per day is 1155.
Rake: Average amount of money, company earns per day is Rs 10137. The maximum amount of money, the company earned in a day is Rs 271200. Most of the time the company earns Rs 3195.
Wager: Maximum amount players invested to play the game is Rs 4520000, whereas most of the time users pay Rs 24000.
That’s pretty much tons of information with just one line of code. Thanks to pandas.
It would be interesting to visualize a few information a step further. We would use matplotlibpython library to perform the same.
Let's start with the total amount paid by the users as entry fees to play the game.
plt.figure(figsize=(9, 8))sns.distplot(df['Wager'], color='g', bins=100, hist_kws={'alpha': 0.4})plt.title("Total amount paid by the users in terms of Entry Fees to play the game")
By the graph above, we observe that most of the time users pay around Rs 25000 on a particular day.
Now let's see the total amount generated in revenue from a table configuration for the date.
plt.figure(figsize=(9, 8))sns.distplot(df['Rake'], color='b', bins=100, hist_kws={'alpha': 0.4})plt.title("Total amount generated in revenue from a table configuration for the date")
By the graph above and basic statistics, it can be realized that most of the time the company earns around Rs 3000 per day.
Buy-in (money user pays) in rupees to enter the game
num_bins = 60plt.hist(df['Entry Fee'], num_bins, facecolor='blue', alpha=0.5)plt.title("Buy-in (money user pays) in rupees to enter the game")plt.show()
We observe that most of the people buy tickets costing less than 1000 Rs. There are some users who spend Rs 10000 as an entry fee to play the game.
Data analysis and visualization gives us insights into the data in-depth and in a few instances we might encounter magic. So let's dive deep into the dataset any perform time series analysis.
Our main focus is on the behavior of the data from time to time.
We would convert the date (which is written alphabetically) to numerical counterpart and create separate columns for date, month and year. It's easy to perform data analysis using numerical data.
We would use datetimepython class to convert date to datetime format. Let’s see how it works.
# introducing datetimefrom datetime import datetime, date, timedatetime.strptime('July 1, 2018', '%B %d, %Y')
Output:
datetime.datetime(2018, 7, 1, 0, 0)
Now we would perform the same operation on our entire Date column.
# converting date to datetime formatdate = []for d in df['Date']:    x = datetime.strptime(d, '%B %d, %Y')    date.append(x)df['my_date'] = date
print(df['my_date'][0].month)print(df['my_date'][0].day)print(df['my_date'][0].year)Output:712018
Converting DateTime to individual columns
# converting datetime to individual columnsday = []month = []year = []for i in df['my_date']:    day.append(i.day)    month.append(i.month)    year.append(i.year)df['day'] = daydf['month'] = monthdf['year'] = yeardf.head(2)
So now that we have converted the Date to individuals columns of date, month and year. Let’s perform a time-series analysis.
#sale by monthsales_by_month = df.groupby('month').size()print(sales_by_month)#Plotting the Graphplot_by_month = sales_by_month.plot(title='Monthly Sales',xticks=(1,2,3,4,5,6,7,8,9,10,11,12))plot_by_month.set_xlabel('Months')plot_by_month.set_ylabel('Total Sales')plt.title("Monthly sales analysis")
It is observed that the number of sales in the month of January is high as compared to other months. As the months progressed the number of sales decreased.
#Sale by Daysales_by_day = df.groupby('day').size()plot_by_day = sales_by_day.plot(title='Daily Sales',xticks=(range(1,31)),rot=55)plot_by_day.set_xlabel('Day')plot_by_day.set_ylabel('Total Sales')plt.title("Daily sales analysis")
We observe that the number of sales in the first five days of the month is high as compared to other months.
We could have analyzed the behavior of the sales even better if we could have got the hourly data.
It’s a good idea to observe the number of users engaged in the game per month. We would use boxplot to visualize the same.
# number of users played per monthsns.boxplot(x='month', y='# Users', data=df)plt.title("Number of users played per month")plt.show()
As time progressed the number of users per month increased gradually. If we focus on the quantiles, in January there were around 100 people on an average per day. But there were around 400 at maximum. In the month of September, there were around 220 users on an average per day whereas there was around 1200 user per day. Same can be understood for other months as well.
# Number of users played in table configuration per month.sns.boxplot(x='month', y='User Cash Game Count', data=df)plt.title("Number of users played in table configuration per month")plt.show()
Similarly, if we focus on the number of games played per month on a table. In January around 100 people played in a day whereas at max 800 people played in a day. We observed that there was a slight decrease in the number of games played in the month of April. In the month of September in an average of 500 people played on the table whereas at max 3000 people played on table, 75% time 900 people played the game on table.
# Profit earned by company every monthsns.boxplot(x='month', y='Rake', data=df)plt.title("Profit earned by company every month")plt.show()
If we observe the boxplot, the company earned most of the profit in the month of September in an average. We also see that maximum profit earned at a time is around 300000 in the month of May. In the month of February, the company earned the least revenue.
# total amount payed by users as entry fee to play games per month.sns.boxplot(x='month', y='Wager', data=df)plt.title("Total amount payed by users as entry fee to play games per month")plt.show()
We observed that same scenario is observed as above RAGE plots. In the month of May, the maximum amount is spent by users to play a game. In an average September has a good record.
Now let's perform day-wise analysis and see if we can get any further important information.
# number of cash games played by users per day. plt.figure(figsize=(20,18))df.plot(kind='scatter', x='day', y='User Cash Game Count');plt.title("Number of cash games played by users per day")plt.show()
# Revenue earned by users in days of monthdf.plot(kind='scatter', x='day', y='Rake');plt.title("Revenue earned by users in days of month")plt.show()
# Amount spent by user every day of the month.df.plot(kind='scatter', x='day', y='Wager');plt.title("Amount spent by user every day of the month")plt.show()
We have performed basic data analysis on the time series data and could get lots of information out of it. It's just the game of data manipulation and inculcating ideas.
Entry Fee: Firstly, the maximum number of people spend 100 Rs to enter the game.
Seat: At maximum 6 players can sit on a table, among them, 3 players play most of the time.
Cut%: In most cases, 15% of the amount is deducted per user.
No of Users: In a day 112 players play at least a table game per day mostly.
User cash game: In most cases, 212 players play table game in a day.
Rake: Per day company earns Rs 3195 in an average.
Wager: Per day users spends Rs 24000 in an average.
We observed that most of the users pay less than Rs 1000 to play a game.
It is observed that the number of sales in the month of January is high as compared to other months. As the months progressed the number of sales decreased.
We observe that the number of sales in the first five days of the month is high as compared to other months.
We observe that the number of users gradually increased as the days progressed. But May month had significant high user engagement from preceding months.
We see that May month had very high revenue generation by the company.
We had observed that every day of the month shown uniform revenue generation by the company.
Overall we observed that the month of May had been best for the company’s profitability. It had shown significant growth in the same. Whereas the month of February was a little bit odd for the company in terms of profitability.
Source code can be found on my Github repo. I look forward to hearing any feedback or questions. Thanks for reading the article.
Follow me on LinkedIn.