<jupyter_start><jupyter_text>Dependencies<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
#here we save tha path of my data
files =  [file for file in os.listdir("/home/monky/Descargas/ciencia_datos/Sales_Data_Analysis/Sales_Data")]#here we create a list with a loop that goes through all data in this path 
for file in files:#here we create a loop that goes through my list of file
    print(file)#here we imprint every file 
# here we convert my data to a dataframe and display all those columns and rows, with "shape"
path = "/home/monky/Descargas/ciencia_datos/Sales_Data_Analysis/Sales_Data"#here we create a varible path
all_data = pd.DataFrame()#here create a dataframe
for file in files:
    current_df = pd.read_csv(path+"/"+file)#here we concatenate mi path and mi file
    all_data = pd.concat([all_data, current_df])## and here we convert my data to a dataframe (and save it in the variable all_data)
    

all_data.shape#here we see all columns and rows for shape
#here we convert mi data in a csv
all_data.to_csv("/home/monky/Descargas/ciencia_datos/Sales_Data_Analysis/Sales_Data/all_data.csv",index = False)
# here we can see the result
all_data.head()<jupyter_output><empty_output><jupyter_text>Analyze sales over the months and see which month is the best in sales<jupyter_code>#first you need to know if you are missing data
all_data.isnull().sum()#here we sum al missing data of all columns 
# here we delete everything that is missing with dropna, with the parameter how = "all"
all_data = all_data.dropna(how = "all")
#here we can see the results 
all_data.isnull().sum()
#here delete everything number after "/" and we only leave 12
"12/30/19 00:01".split("/")[0]# here we specify what number we want (if you want 30, put [1] instead of [0])
#here we create a simple function
def month(x):
    return x.split("/")[0]#this returns the month (or in other words, the first numbers before "/")
# here we have the column of the month with the month of each sale
all_data["month"] = all_data["Order Date"].apply(month)
#here we can see the result 
all_data.head()
# here we can see that there is an unexpected value in my column month (Order Date)
all_data["month"].unique()
# here we delete that unexpected value
filter = all_data["month"] == "Order Date"#here we create a "filter"(that is the value "Order Date" in the column month)
all_data = all_data[-filter]#Here and here we use our filter.
all_data.head()#here we can see the result
# here we see what type of data is in each column
all_data.dtypes
#here convert to int a column month
all_data["month"] = all_data["month"].astype(int)
#here we can see the result
all_data.dtypes
#here we convert a Quantity Ordered in init too 
all_data["Quantity Ordered"] = all_data["Quantity Ordered"].astype(int)
#here we convert a Price Each in a float
all_data["Price Each"] = all_data["Price Each"].astype(float)
#here we can see the result
all_data.dtypes
# here we create a column "sales" to represent the money earned in a month 
all_data["sales"] = all_data["Quantity Ordered"]*all_data["Price Each"]# here we multiply the "Price each" by the "Quantity ordered"
#here we can see the result
all_data.head()
#here we agrup the month and the sales
all_data.groupby("month")["sales"].sum()
# here we create a graph to see how much money you made in a month
months = range (1, 13)#here we create a variable with a range of 1 to 13 (the month 1 to the month 12) 

#here we create the graph with my variable month and with the group of my columns (month and sales)
plt.bar(months, all_data.groupby("month")["sales"].sum())
plt.xticks(months)

plt.xlabel("month")# here we create a name for the x-axis
plt.ylabel("Sales in USD")#here we create a name for the y-axis<jupyter_output><empty_output><jupyter_text>Which city has max order<jupyter_code>#first we see our data frame
all_data.head()
#now now we need to separate the city from the other data in my column "Purchase address"
"136 Church St, New York City, NY 10001".split(",")[1]#We use the split function and we pass it as a parameter the commas (,) and we ask for the return of the second value [1] (yes, 1 is the second value by 0)
#now we create(city) a function to automate this
def city(x):
    return x.split(",")[1]
#now we use the city function a city column 
all_data["city"] = all_data["Purchase Address"].apply(city)
#here we can see tr results
all_data.head()
#Now we make a graph to see in which city to buy more.
#basically we join the name of the city with the count of times it appears in my dataframe
all_data.groupby("city")["city"].count().plot.bar()<jupyter_output><empty_output><jupyter_text>At what time are product purchase sales maximum?<jupyter_code>#here we can see that column "Order Date" is not a date type
all_data["Order Date"].dtype
# so we create a column "Time", we use the data of the "Order Date" but we convert the data to the date type
# and only use the hour not date of day
all_data["Hour"] = pd.to_datetime(all_data["Order Date"]).dt.hour
all_data.head()
#here we create two list, "keys" is for hour of the day and"Hour" have the len of the hour_df
keys = []
hour = []
#here we fill in those lists
for key,hour_df in all_data.groupby("Hour"):
    keys.append(key)
    hour.append(len(hour_df))
#here we can see the result
hour
#here we can see the result
keys
#and here we make a graph with my data
plt.grid()#here we customize a graphic
plt.plot(keys,hour)#here create a graph<jupyter_output><empty_output><jupyter_text>Which product sold the most and why<jupyter_code># here we group products with the quantity ordered, and sum the data
#after creating a chart with this data
all_data.groupby("Product")["Quantity Ordered"].sum().plot(kind = "bar")
#here we group the Product data and the Price Each data
#we this data and the function "mean" we can calculate the median Price Each product
all_data.groupby("Product")["Price Each"].mean()
# here we group the requested quantity data with the product name data, and convert into an index
products = all_data.groupby("Product")["Quantity Ordered"].sum().index
# here we group the requested quantity data with the product name data
quantity = all_data.groupby("Product")["Quantity Ordered"].sum()
#here we group the Price Each product data with the product name 
prices = all_data.groupby("Product")["Price Each"].mean()

#here we create a grahp 
fig,ax1 = plt.subplots()

#here we create a 2xis, this is a copy of the 1xis but this uses the twinx () function, this function can make two data coexist at the same index
ax2 = ax1.twinx()

#here we the agroup the name products(in the axis x, like a index) with the Quantity Ordered(in the axis y) 
ax1.bar(products,quantity, color = "g")#with "g" we set the color of the graph in green 

#here we the agroup the name products(in the axis x, like a index) with the prices(in the axis y) 
ax2.plot(products, prices)

#here we set the index(name products) vertically (horizontally it doesn't look good)
ax1.set_xticklabels(products, rotation = "vertical", size = 8)<jupyter_output><ipython-input-40-8732a33aa584>:5: UserWarning: FixedFormatter should only be used together with FixedLocator
  ax1.set_xticklabels(products, rotation = "vertical", size = 8)<jupyter_text>What products are most often sold together<jupyter_code>all_data.head()
df = all_data["Order ID"].duplicated(keep = False)

df2 = all_data[df]

df2.head()
#here we create a new column in my datafrae "Grouped" this columns agroup a the products purchased in the same purchase 
df2["Grouped"] = df2.groupby("Order ID")["Product"].transform(lambda x:",".join(x))
df2.head()
#here delete the duplicates ids in sort datafram
df2 = df2.drop_duplicates(subset = ["Order ID"])
#here we see the result
df2.head()
#here we agroup value_counts() in the column "Grouped"
df2["Grouped"].value_counts()
#here we create a graph with the top 5 shopping together
df2["Grouped"].value_counts()[0:5].plot.pie()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09774718397997496, 'fraction_numerical': 0.00951188986232791, 'mean_word_length': 5.090701219512195, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '228805', 'license': 'MIT', 'n_tokens_mistral': 2546, 'n_tokens_neox': 2465, 'n_words': 1074, 'path': 'Sales Data Analysis.ipynb', 'repo_name': 'Simon-Lozada/Sales_Data_Analysis', 'size': '7990'}