0% found this document useful (0 votes)
26 views

Online Reatil Data

The document discusses analyzing an online retail dataset using Python. It loads necessary libraries, reads in an Excel file, cleans the data by removing times and filling null values, and sets up a Flask API to query the data and return a subset of rows for a specific customer ID. The API is served locally on port 5000.

Uploaded by

akash.050501
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Online Reatil Data

The document discusses analyzing an online retail dataset using Python. It loads necessary libraries, reads in an Excel file, cleans the data by removing times and filling null values, and sets up a Flask API to query the data and return a subset of rows for a specific customer ID. The API is served locally on port 5000.

Uploaded by

akash.050501
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Importing necessary libraries

In [1]:

import pandas as pd
from flask import Flask
import pymysql

Flask
In [2]:

app = Flask(__name__)

Reading the dataset


In [3]:

df=pd.read_excel('Online Retail.xlsx')
df.head()

Out[3]:

InvoiceNo StockCode Description Quantity InvoiceDate UnitPrice CustomerID Country

WHITE HANGING HEART T-LIGHT 2010-12-01 United


0 536365 85123A 6 2.55 17850.0
HOLDER 08:26:00 Kingdom

2010-12-01 United
1 536365 71053 WHITE METAL LANTERN 6 3.39 17850.0
08:26:00 Kingdom

CREAM CUPID HEARTS COAT 2010-12-01 United


2 536365 84406B 8 2.75 17850.0
HANGER 08:26:00 Kingdom

KNITTED UNION FLAG HOT WATER 2010-12-01 United


3 536365 84029G 6 3.39 17850.0
BOTTLE 08:26:00 Kingdom

2010-12-01 United
4 536365 84029E RED WOOLLY HOTTIE WHITE HEART. 6 3.39 17850.0
08:26:00 Kingdom

In [4]:
df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 541909 entries, 0 to 541908
Data columns (total 8 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 InvoiceNo 541909 non-null object
1 StockCode 541909 non-null object
2 Description 540455 non-null object
3 Quantity 541909 non-null int64
4 InvoiceDate 541909 non-null datetime64[ns]
5 UnitPrice 541909 non-null float64
6 CustomerID 406829 non-null float64
7 Country 541909 non-null object
dtypes: datetime64[ns](1), float64(2), int64(1), object(4)
memory usage: 33.1+ MB

Remove time from date


In [5]:
In [5]:
df['InvoiceDate']=pd.to_datetime(df['InvoiceDate'],format="%m/%d /%Y/ %H:%M").dt.date
df.head()
Out[5]:

InvoiceNo StockCode Description Quantity InvoiceDate UnitPrice CustomerID Country

WHITE HANGING HEART T-LIGHT United


0 536365 85123A 6 2010-12-01 2.55 17850.0
HOLDER Kingdom

United
1 536365 71053 WHITE METAL LANTERN 6 2010-12-01 3.39 17850.0
Kingdom

United
2 536365 84406B CREAM CUPID HEARTS COAT HANGER 8 2010-12-01 2.75 17850.0
Kingdom

KNITTED UNION FLAG HOT WATER United


3 536365 84029G 6 2010-12-01 3.39 17850.0
BOTTLE Kingdom

United
4 536365 84029E RED WOOLLY HOTTIE WHITE HEART. 6 2010-12-01 3.39 17850.0
Kingdom

Checking for null values


In [6]:
df.isnull().sum()
Out[6]:
InvoiceNo 0
StockCode 0
Description 1454
Quantity 0
InvoiceDate 0
UnitPrice 0
CustomerID 135080
Country 0
dtype: int64

Treating null values


In [7]:
df=df.dropna(subset=['CustomerID'])
df.isnull().sum()
Out[7]:
InvoiceNo 0
StockCode 0
Description 0
Quantity 0
InvoiceDate 0
UnitPrice 0
CustomerID 0
Country 0
dtype: int64

Converting the df to csv


In [8]:
#df.to_csv('data.csv', index=False)

API
API
In [9]:
app = Flask(__name__)

# MySQL Configuration
db_config = {
'host': 'localhost',
'user': 'root',
'password': '050501@Aks',
'database': 'akashdb',
}

# Initialize MySQL connector


db = pymysql.connect(**db_config)
cursor = db.cursor()

@app.route('/api/data', methods=['GET'])
def get_users():
try:
# Execute a simple query to retrieve users from the table
cursor.execute("SELECT * FROM data where CustomerID=17850 limit 20")
users = cursor.fetchall()

# Convert the results to a newline-separated string with HTML line breaks


user_list = '<br>'.join([f'{user[0]}, {user[1]}, {user[2]}, {user[3]}, {user[4]}
, {user[5]}, {user[6]}, {user[7]}' for user in users])

return user_list

except Exception as e:
return str(e)

if __name__ == '__main__':
app.run(debug=False)

* Serving Flask app '__main__'


* Debug mode: off

WARNING: This is a development server. Do not use it in a production deployment. Use a pr


oduction WSGI server instead.
* Running on http://127.0.0.1:5000
Press CTRL+C to quit
127.0.0.1 - - [30/Nov/2023 12:40:25] "GET /api/data HTTP/1.1" 200 -

In [10]:
# Link : http://127.0.0.1:5000/api/data

You might also like