Sales Dataset Analysis
Sales Dataset Analysis
import numpy as np
import pandas as pd
import os
So basically in dataset we have 12 months of data separately in each csv file so first we
have to merge the data to get the yearly data to make analysis easy for us
#THROUGH listdir() we can get to all files
files = [file for file in os.listdir('./Sales_Data')]
Sales_April_2019.csv
Sales_August_2019.csv
Sales_December_2019.csv
Sales_February_2019.csv
Sales_January_2019.csv
Sales_July_2019.csv
Sales_June_2019.csv
Sales_March_2019.csv
Sales_May_2019.csv
Sales_November_2019.csv
Sales_October_2019.csv
Sales_September_2019.csv
all_months_data = pd.DataFrame()
#now concatenating
year_df = pd.read_csv('./year_data.csv')
year_df.head()
year_df.columns
year_df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 186850 entries, 0 to 186849
Data columns (total 6 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Order ID 186305 non-null object
1 Product 186305 non-null object
2 Quantity Ordered 186305 non-null object
3 Price Each 186305 non-null object
4 Order Date 186305 non-null object
5 Purchase Address 186305 non-null object
dtypes: object(6)
memory usage: 8.6+ MB
year_df.describe()
#we have date but not month month is coving first two indecies of date
#firstly we will convert date into string so that we can get its first
two characeters
year_df.head()
#we will use apply() method that basically helps us to apply any
arbitaray function on dataframe
#we can also do that with a function but still we will use apply
def get_city(adress):
return adress.split(',')[1]
year_df['City'].head()
0 Dallas
2 Boston
3 Los Angeles
4 Los Angeles
5 Los Angeles
Name: City, dtype: object
We have to also mention the state with city because in the dataset we have many cities with
same name, so state is also important
def get_state(adress):
#by splitting on commas and getting second index element we will
get state and postal code also
state_postal = adress.split(',')[2]
#so we have to split again on space and get ist index to properly
get the state
#so on the left side of space we have white space that is covering
the 0th index so that is why we got state of out of
#ist index
0 Dallas TX
2 Boston MA
3 Los Angeles CA
4 Los Angeles CA
5 Los Angeles CA
Name: City, dtype: object
Data Cleaning
nan_df.head()
Month
1 NaN
356 NaN
735 NaN
1433 NaN
1553 NaN
#it takes parameter how --> that can have two values any and all
#any means even if even 1 column has NaN
#all means it will drop the row that has all values NaN
months = ['01','02''03','04','05','06','07','08','09','10','11','12']
or_df = year_df['Month'].isin(months)
#the result variable will contain a pandas Series object with boolean
values that
#indicate whether each row in the 'Month' column has a value from the
check_list
#so lets get those values that has some other values
false_rows = year_df.Month[~or_df]
print(false_rows)
519 Or
1149 Or
1155 Or
2878 Or
2893 Or
..
185164 Or
185551 Or
186563 Or
186632 Or
186738 Or
Name: Month, Length: 27483, dtype: object
year_df.head()
0 4
2 4
3 4
4 4
5 4
Name: Month, dtype: int32
year_df['Sales'].head()
0 23.90
2 99.99
3 600.00
4 11.99
5 11.99
Name: Sales, dtype: float64
Analysis
Month
1 1822256.73
2 2202022.42
3 2807100.38
4 3390670.24
5 3152606.75
6 2577802.26
7 2647775.76
8 2244467.88
9 2097560.13
10 3736726.88
11 3199603.20
12 4613443.34
Name: Sales, dtype: float64
sales_every_month.max()
4613443.34
Plotting
import matplotlib.pyplot as plt
#xaxis
months = range(1,13)
plt.bar(months, sales_every_month);
plt.xticks(months);
plt.xlabel('Month');
plt.ylabel('Sales in USD $');
City
0 Dallas TX
2 Boston MA
3 Los Angeles CA
4 Los Angeles CA
5 Los Angeles CA
City
Atlanta GA 2795498.58
Austin TX 1819581.75
Boston MA 3661642.01
Dallas TX 2767975.40
Los Angeles CA 5452570.80
New York City NY 4664317.43
Portland ME 449758.27
Portland OR 1870732.34
San Francisco CA 8262203.91
Seattle WA 2747755.48
Name: Sales, dtype: float64
sales_of_cities.max()
8262203.91
City
0 Dallas TX
2 Boston MA
3 Los Angeles CA
4 Los Angeles CA
5 Los Angeles CA
#for getting time that we have in Order Date, we can split and get it
due formatting issues
year_df['Order Date'].head()
0 2019-04-19 08:46:00
2 2019-04-07 22:30:00
3 2019-04-12 14:38:00
4 2019-04-12 14:38:00
5 2019-04-30 09:27:00
Name: Order Date, dtype: datetime64[ns]
hour
0 713721.27
1 460866.88
2 234851.44
3 145757.89
4 162661.01
5 230679.82
6 448113.00
7 744854.12
8 1192348.97
9 1639030.58
10 1944286.77
11 2300610.24
12 2316821.34
13 2155389.80
14 2083672.73
15 1941549.60
16 1904601.31
17 2129361.61
18 2219348.30
19 2412938.54
20 2281716.24
21 2042000.86
22 1607549.21
23 1179304.44
Name: Sales, dtype: float64
sales_with_hours.max()
2412938.54
plt.plot(hours, sales_with_hours);
plt.xticks(hours)
plt.grid()
plt.xlabel('Hour')
plt.ylabel('Sales')
#so what count do is it will count the number of rows where specific
hour will occur and that shows order took place at that hour
#So the orders that have same order Ids are sold together like at
index 3 and 4
#so this dataframe will have only those rows where the Order ID is
repeated (once, twice or ....)
dup_df.head(10)
119 2019-04-12 11:07:00 778 Maple St, New York City, NY 10001 4
120 2019-04-12 11:07:00 778 Maple St, New York City, NY 10001 4
#so now what we need is, we will make seprate column where we will
have products (having same ID) in the same the Row
# so we will use groupby() to group Order Id and then we will use
tranform() to basically join the rows on the base grouped Ids :)
C:\Users\DELL\AppData\Local\Temp\ipykernel_6084\3824790320.py:8:
SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
Order ID Product \
3 176560 Google Phone
4 176560 Wired Headphones
18 176574 Google Phone
19 176574 USB-C Charging Cable
30 176585 Bose SoundSport Headphones
31 176585 Bose SoundSport Headphones
32 176586 AAA Batteries (4-pack)
33 176586 Google Phone
119 176672 Lightning Charging Cable
120 176672 USB-C Charging Cable
Grouped Product
3 Google Phone,Wired Headphones
4 Google Phone,Wired Headphones
18 Google Phone,USB-C Charging Cable
19 Google Phone,USB-C Charging Cable
30 Bose SoundSport Headphones,Bose SoundSport Hea...
31 Bose SoundSport Headphones,Bose SoundSport Hea...
32 AAA Batteries (4-pack),Google Phone
33 AAA Batteries (4-pack),Google Phone
119 Lightning Charging Cable,USB-C Charging Cable
120 Lightning Charging Cable,USB-C Charging Cable
df = df.drop_duplicates()
df.head()
count = Counter()
#now we have to iterate over every row of "Grouped Product"
for row in df['Grouped Product']:
#now we will split on ','
row_list = row.split(',')
#so this line below will count the Combination as they will appear
in the dataframe
#so 2 here shows that it will
count those orders where 2 items are ordered together again and again
count.update(Counter(combinations(row_list, 2)))
sold_item
Product
20in Monitor 4129
27in 4K Gaming Monitor 6244
27in FHD Monitor 7550
34in Ultrawide Monitor 6199
AA Batteries (4-pack) 27635
AAA Batteries (4-pack) 31017
Apple Airpods Headphones 15661
Bose SoundSport Headphones 13457
Flatscreen TV 4819
Google Phone 5532
LG Dryer 646
LG Washing Machine 666
Lightning Charging Cable 23217
Macbook Pro Laptop 4728
ThinkPad Laptop 4130
USB-C Charging Cable 23975
Vareebadd Phone 2068
Wired Headphones 20557
iPhone 6849
Name: Quantity Ordered, dtype: int32
sold_item.max()
31017
plt.bar(items, sold_item)
plt.xticks(items, rotation = 'vertical');
plt.xlabel('Items');
plt.ylabel('Number of Orders of Each');
#So to make it more clear basically we can find price of each product
and plot it with its number of times it got order
price_of_each_item
Product
20in Monitor 109.99
27in 4K Gaming Monitor 389.99
27in FHD Monitor 149.99
34in Ultrawide Monitor 379.99
AA Batteries (4-pack) 3.84
AAA Batteries (4-pack) 2.99
Apple Airpods Headphones 150.00
Bose SoundSport Headphones 99.99
Flatscreen TV 300.00
Google Phone 600.00
LG Dryer 600.00
LG Washing Machine 600.00
Lightning Charging Cable 14.95
Macbook Pro Laptop 1700.00
ThinkPad Laptop 999.99
USB-C Charging Cable 11.95
Vareebadd Phone 400.00
Wired Headphones 11.99
iPhone 700.00
Name: Price Each, dtype: float64
#for platting
fig,ax1 = plt.subplots()
ax2 = ax1.twinx()
#so our 1 y axis will sold_items
ax1.bar(items, sold_item, color = 'green')
#and other y axis will have price of items
ax2.plot(items, price_of_each_item)
C:\Users\DELL\AppData\Local\Temp\ipykernel_6084\1827761392.py:11:
UserWarning: FixedFormatter should only be used together with
FixedLocator
ax1.set_xticklabels(items, rotation = 'vertical');
So you can see why AAA Batteries have so much orders because its price is very low on the
other hand things that have high price, have less number of orders :)