diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..e931a8c Binary files /dev/null and b/.DS_Store differ diff --git a/Database_ (1).ipynb b/Database_ (1).ipynb deleted file mode 100644 index 3c7512d..0000000 --- a/Database_ (1).ipynb +++ /dev/null @@ -1,596 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 14, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
order_idcustomer_idorder_statusorder_purchase_timestamporder_approved_atorder_delivered_carrier_dateorder_delivered_customer_dateorder_estimated_delivery_datepayment_typepayment_installments...seller_idshipping_limit_datepricefreight_valueseller_cityseller_stateproduct_category_nameproduct_name_lenghtproduct_description_lenghtproduct_category_name_english
053cdb2fc8bc7dce0b6741e2150273451b0830fb4747a6c6d20dea0b8c802d7efdelivered2018-07-24 20:41:372018-07-26 03:24:272018-07-26 14:31:002018-08-07 15:27:452018-08-13 00:00:00boleto1...289cdb325fb7e7f891c38608bf9e09622018-07-30 03:24:27118.722.76belo horizonteSPperfumaria29178perfumery
186674ccaee19790309333210917b2c7d1b338293f35549b5e480b9a3d7bbf3cddelivered2018-08-09 11:37:352018-08-09 14:35:192018-08-10 14:34:002018-08-14 18:51:472018-08-22 00:00:00credit_card5...289cdb325fb7e7f891c38608bf9e09622018-08-13 14:31:29116.918.92belo horizonteSPperfumaria29178perfumery
2aee682982e18eb4714ce9f97b15af5e28858442ea4d5dc5bb9e118e8f728095ddelivered2018-07-09 18:46:282018-07-11 03:45:452018-07-11 15:01:002018-07-12 18:14:352018-07-18 00:00:00boleto1...289cdb325fb7e7f891c38608bf9e09622018-07-13 03:45:45118.79.34belo horizonteSPperfumaria29178perfumery
3d543201a9b42a1402ff97e65b439a48b971bf8f42a9f8cb3ead257854905b454delivered2018-08-21 10:00:252018-08-21 10:50:542018-08-22 15:21:002018-08-28 18:58:222018-09-10 00:00:00credit_card2...289cdb325fb7e7f891c38608bf9e09622018-08-23 10:50:54116.922.75belo horizonteSPperfumaria29178perfumery
4d543201a9b42a1402ff97e65b439a48b971bf8f42a9f8cb3ead257854905b454delivered2018-08-21 10:00:252018-08-21 10:50:542018-08-22 15:21:002018-08-28 18:58:222018-09-10 00:00:00credit_card2...289cdb325fb7e7f891c38608bf9e09622018-08-23 10:50:54116.922.75belo horizonteSPperfumaria29178perfumery
\n", - "

5 rows × 26 columns

\n", - "
" - ], - "text/plain": [ - " order_id customer_id \\\n", - "0 53cdb2fc8bc7dce0b6741e2150273451 b0830fb4747a6c6d20dea0b8c802d7ef \n", - "1 86674ccaee19790309333210917b2c7d 1b338293f35549b5e480b9a3d7bbf3cd \n", - "2 aee682982e18eb4714ce9f97b15af5e2 8858442ea4d5dc5bb9e118e8f728095d \n", - "3 d543201a9b42a1402ff97e65b439a48b 971bf8f42a9f8cb3ead257854905b454 \n", - "4 d543201a9b42a1402ff97e65b439a48b 971bf8f42a9f8cb3ead257854905b454 \n", - "\n", - " order_status order_purchase_timestamp order_approved_at \\\n", - "0 delivered 2018-07-24 20:41:37 2018-07-26 03:24:27 \n", - "1 delivered 2018-08-09 11:37:35 2018-08-09 14:35:19 \n", - "2 delivered 2018-07-09 18:46:28 2018-07-11 03:45:45 \n", - "3 delivered 2018-08-21 10:00:25 2018-08-21 10:50:54 \n", - "4 delivered 2018-08-21 10:00:25 2018-08-21 10:50:54 \n", - "\n", - " order_delivered_carrier_date order_delivered_customer_date \\\n", - "0 2018-07-26 14:31:00 2018-08-07 15:27:45 \n", - "1 2018-08-10 14:34:00 2018-08-14 18:51:47 \n", - "2 2018-07-11 15:01:00 2018-07-12 18:14:35 \n", - "3 2018-08-22 15:21:00 2018-08-28 18:58:22 \n", - "4 2018-08-22 15:21:00 2018-08-28 18:58:22 \n", - "\n", - " order_estimated_delivery_date payment_type payment_installments ... \\\n", - "0 2018-08-13 00:00:00 boleto 1 ... \n", - "1 2018-08-22 00:00:00 credit_card 5 ... \n", - "2 2018-07-18 00:00:00 boleto 1 ... \n", - "3 2018-09-10 00:00:00 credit_card 2 ... \n", - "4 2018-09-10 00:00:00 credit_card 2 ... \n", - "\n", - " seller_id shipping_limit_date price freight_value \\\n", - "0 289cdb325fb7e7f891c38608bf9e0962 2018-07-30 03:24:27 118.7 22.76 \n", - "1 289cdb325fb7e7f891c38608bf9e0962 2018-08-13 14:31:29 116.9 18.92 \n", - "2 289cdb325fb7e7f891c38608bf9e0962 2018-07-13 03:45:45 118.7 9.34 \n", - "3 289cdb325fb7e7f891c38608bf9e0962 2018-08-23 10:50:54 116.9 22.75 \n", - "4 289cdb325fb7e7f891c38608bf9e0962 2018-08-23 10:50:54 116.9 22.75 \n", - "\n", - " seller_city seller_state product_category_name product_name_lenght \\\n", - "0 belo horizonte SP perfumaria 29 \n", - "1 belo horizonte SP perfumaria 29 \n", - "2 belo horizonte SP perfumaria 29 \n", - "3 belo horizonte SP perfumaria 29 \n", - "4 belo horizonte SP perfumaria 29 \n", - "\n", - " product_description_lenght product_category_name_english \n", - "0 178 perfumery \n", - "1 178 perfumery \n", - "2 178 perfumery \n", - "3 178 perfumery \n", - "4 178 perfumery \n", - "\n", - "[5 rows x 26 columns]" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "import pandas as pd\n", - "data = pd.read_csv('customer_segmentation.csv')\n", - "data.head()" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[('customersdatabase',), ('customersdatabase2',), ('goshare',), ('information_schema',), ('mysql',), ('performance_schema',), ('sakila',), ('sys',), ('world',)]\n", - "Database is created\n" - ] - } - ], - "source": [ - "import mysql.connector as mysql\n", - "from mysql.connector import Error \n", - "\n", - "db_name='CustomersDatabaseFinal'\n", - "try:\n", - " mydb = mysql.connect(host='localhost', user='Alessandro', password='Terracina1!', auth_plugin='mysql_native_password') # you can add the auth_plugin here too (ref line 26)\n", - " if mydb.is_connected():\n", - " mycursor = mydb.cursor()\n", - " mycursor.execute('SHOW DATABASES')\n", - " result = mycursor.fetchall()\n", - " print(result)\n", - " for x in result:\n", - " if db_name == x[0]:\n", - " mycursor.execute('DROP DATABASE ' + db_name) # delete old database\n", - " mydb.commit() # make the changes official\n", - " print(\"The database already exists! The old database has been deleted!)\")\n", - " \n", - " mycursor.execute(\"CREATE DATABASE \"+ db_name)\n", - " print(\"Database is created\")\n", - "except Error as e:\n", - " print(\"Error while connecting to MySQL\", e)\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "141.46" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "dataset = pd.read_csv('customer_segmentation.csv')\n", - "dataset.head()\n", - "dataset['payment_value'][0]" - ] - }, - { - "cell_type": "code", - "execution_count": 35, - "metadata": {}, - "outputs": [], - "source": [ - "mycursor.execute(\"USE CustomersDatabaseFinal\")\n" - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "metadata": {}, - "outputs": [], - "source": [ - "mycursor.execute(\n", - " ''' \n", - " CREATE TABLE customer(\n", - " customer_unique_id VARCHAR(40),\n", - " customer_id VARCHAR(40),\n", - " customer_city VARCHAR(40),\n", - " customer_state VARCHAR(40),\n", - " PRIMARY KEY (customer_unique_id)\n", - " );\n", - " '''\n", - " )" - ] - }, - { - "cell_type": "code", - "execution_count": 37, - "metadata": {}, - "outputs": [], - "source": [ - "mycursor.execute(\n", - " '''\n", - " CREATE TABLE seller(\n", - " seller_id VARCHAR(40),\n", - " seller_city VARCHAR(40),\n", - " seller_state VARCHAR(40),\n", - " PRIMARY KEY (seller_id)\n", - " );\n", - "\n", - "\n", - " '''\n", - "\n", - "\n", - "\n", - "\n", - "\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": 38, - "metadata": {}, - "outputs": [], - "source": [ - "mycursor.execute(\n", - " '''\n", - " CREATE TABLE product(\n", - " product_id VARCHAR(40),\n", - " price int,\n", - " freight_value int,\n", - " product_category_name VARCHAR(60),\n", - " product_description_lenght int,\n", - " PRIMARY KEY (product_id)\n", - " );\n", - "\n", - "\n", - " '''\n", - "\n", - "\n", - "\n", - "\n", - "\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": 39, - "metadata": {}, - "outputs": [], - "source": [ - "\n", - "\n", - "mycursor.execute(\n", - " '''\n", - " CREATE TABLE order_(\n", - " order_id VARCHAR(40),\n", - " order_status VARCHAR(40),\n", - " order_purchase_time DATETIME,\n", - " order_approved_at DATETIME,\n", - " order_delivered_carrier_date DATETIME,\n", - " order_delivered_customer_date DATETIME,\n", - " order_estimated_delivery_date DATETIME,\n", - " payment_type VARCHAR(40),\n", - " payment_value int,\n", - " payment_installments int,\n", - " order_customer VARCHAR(40),\n", - " order_product VARCHAR(40),\n", - " order_seller VARCHAR(40),\n", - " PRIMARY KEY (order_id),\n", - " FOREIGN KEY (order_customer) REFERENCES customer(customer_unique_id),\n", - " FOREIGN KEY (order_seller) REFERENCES seller(seller_id),\n", - " FOREIGN KEY (order_product) REFERENCES product(product_id)\n", - " ); \n", - "\n", - " '''\n", - "\n", - "\n", - "\n", - "\n", - "\n", - ")\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 40, - "metadata": {}, - "outputs": [], - "source": [ - "mycursor.execute(\n", - " '''\n", - " CREATE TABLE offer(\n", - " id VARCHAR(40),\n", - " seller_id VARCHAR(40),\n", - " product_id VARCHAR(40),\n", - " PRIMARY KEY (id),\n", - " FOREIGN KEY (seller_id) REFERENCES seller(seller_id),\n", - " FOREIGN KEY (product_id) REFERENCES product(product_id)\n", - " ); \n", - " '''\n", - "\n", - "\n", - "\n", - "\n", - "\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": 44, - "metadata": {}, - "outputs": [], - "source": [ - "for i,row in data.iterrows():\n", - " sql = \"INSERT IGNORE INTO CustomersDatabaseFinal.customer VALUES (%s,%s,%s,%s)\"\n", - " mycursor.execute(sql, tuple([row['customer_unique_id'], row['customer_id'], row['customer_city'], row['customer_state']]))\n", - " #print(\"Record inserted\")\n", - " mydb.commit()" - ] - }, - { - "cell_type": "code", - "execution_count": 45, - "metadata": {}, - "outputs": [], - "source": [ - "for i,row in data.iterrows():\n", - " sql = \"INSERT IGNORE INTO CustomersDatabaseFinal.seller VALUES (%s,%s,%s)\"\n", - " mycursor.execute(sql, tuple([row['seller_id'], row['seller_city'], row['seller_state']]))\n", - " #print(\"Record inserted\")\n", - " mydb.commit()\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [], - "source": [ - "df2 = data.loc[:,['product_id','seller_id']]\n", - "df2.drop_duplicates(inplace = True)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "mycursor.execute(\"ALTER TABLE product DROP COLUMN product_seller\")" - ] - }, - { - "cell_type": "code", - "execution_count": 46, - "metadata": {}, - "outputs": [], - "source": [ - "for i,row in data.iterrows():\n", - " sql = \"INSERT IGNORE INTO CustomersDatabaseFinal.product VALUES (%s,%s,%s,%s,%s)\"\n", - " mycursor.execute(sql, tuple([row['product_id'], row['price'], row['freight_value'], row['product_category_name'], row['product_description_lenght']]))\n", - " #print(\"Record inserted\")\n", - " mydb.commit()\n", - " " - ] - }, - { - "cell_type": "code", - "execution_count": 52, - "metadata": {}, - "outputs": [], - "source": [ - "\n", - "for i,row in data.iterrows():\n", - " sql = \"INSERT IGNORE INTO CustomersDatabaseFinal.order_ VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)\"\n", - " mycursor.execute(sql, tuple([row['order_id'], row['order_status'], row['order_purchase_timestamp'], row['order_approved_at'], row['order_delivered_carrier_date'], row['order_delivered_customer_date'], row['order_estimated_delivery_date'], row['payment_type'], row['payment_value'], row['payment_installments'], row['customer_unique_id'], row['product_id'] , row['seller_id']]))\n", - " #print(\"Record inserted\")\n", - " mydb.commit()" - ] - }, - { - "cell_type": "code", - "execution_count": 50, - "metadata": {}, - "outputs": [], - "source": [ - "c = 0\n", - "for i,row in df2.iterrows():\n", - " sql = \"INSERT IGNORE INTO CustomersDatabaseFinal.offer VALUES (%s,%s,%s)\"\n", - " mycursor.execute(sql, tuple([c, row['seller_id'], row['product_id']]))\n", - " c+=1\n", - " #print(\"Record inserted\")\n", - " mydb.commit()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "ename": "ProgrammingError", - "evalue": "1146 (42S02): Table 'customersdatabase.offers' doesn't exist", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mMySQLInterfaceError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32mc:\\Users\\Tommy\\anaconda3\\lib\\site-packages\\mysql\\connector\\connection_cext.py\u001b[0m in \u001b[0;36mcmd_query\u001b[1;34m(self, query, raw, buffered, raw_as_string)\u001b[0m\n\u001b[0;32m 564\u001b[0m \u001b[0mquery\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mquery\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mencode\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"utf-8\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 565\u001b[1;33m self._cmysql.query(\n\u001b[0m\u001b[0;32m 566\u001b[0m \u001b[0mquery\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", - "\u001b[1;31mMySQLInterfaceError\u001b[0m: Table 'customersdatabase.offers' doesn't exist", - "\nThe above exception was the direct cause of the following exception:\n", - "\u001b[1;31mProgrammingError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_10964\\3945750071.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0my\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mmycursor\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexecute\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"SELECT * FROM OFFERS\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;32mc:\\Users\\Tommy\\anaconda3\\lib\\site-packages\\mysql\\connector\\cursor_cext.py\u001b[0m in \u001b[0;36mexecute\u001b[1;34m(self, operation, params, multi)\u001b[0m\n\u001b[0;32m 277\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 278\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 279\u001b[1;33m result = self._cnx.cmd_query(\n\u001b[0m\u001b[0;32m 280\u001b[0m \u001b[0mstmt\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 281\u001b[0m \u001b[0mraw\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_raw\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", - "\u001b[1;32mc:\\Users\\Tommy\\anaconda3\\lib\\site-packages\\mysql\\connector\\connection_cext.py\u001b[0m in \u001b[0;36mcmd_query\u001b[1;34m(self, query, raw, buffered, raw_as_string)\u001b[0m\n\u001b[0;32m 571\u001b[0m )\n\u001b[0;32m 572\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mMySQLInterfaceError\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0merr\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 573\u001b[1;33m raise get_mysql_exception(\n\u001b[0m\u001b[0;32m 574\u001b[0m \u001b[0merr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0merrno\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmsg\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0merr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmsg\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msqlstate\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0merr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msqlstate\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 575\u001b[0m ) from err\n", - "\u001b[1;31mProgrammingError\u001b[0m: 1146 (42S02): Table 'customersdatabase.offers' doesn't exist" - ] - } - ], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3.10.6 ('spyder-env')", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.10.6" - }, - "orig_nbformat": 4, - "vscode": { - "interpreter": { - "hash": "56eeda6514f840d397735a62262def7a88f09e03751507b2b592ff0725e11aa6" - } - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/Database_.ipynb b/Database_.ipynb deleted file mode 100644 index 64a034c..0000000 --- a/Database_.ipynb +++ /dev/null @@ -1,455 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
order_idcustomer_idorder_statusorder_purchase_timestamporder_approved_atorder_delivered_carrier_dateorder_delivered_customer_dateorder_estimated_delivery_datepayment_typepayment_installments...seller_idshipping_limit_datepricefreight_valueseller_cityseller_stateproduct_category_nameproduct_name_lenghtproduct_description_lenghtproduct_category_name_english
053cdb2fc8bc7dce0b6741e2150273451b0830fb4747a6c6d20dea0b8c802d7efdelivered2018-07-24 20:41:372018-07-26 03:24:272018-07-26 14:31:002018-08-07 15:27:452018-08-13 00:00:00boleto1...289cdb325fb7e7f891c38608bf9e09622018-07-30 03:24:27118.722.76belo horizonteSPperfumaria29178perfumery
186674ccaee19790309333210917b2c7d1b338293f35549b5e480b9a3d7bbf3cddelivered2018-08-09 11:37:352018-08-09 14:35:192018-08-10 14:34:002018-08-14 18:51:472018-08-22 00:00:00credit_card5...289cdb325fb7e7f891c38608bf9e09622018-08-13 14:31:29116.918.92belo horizonteSPperfumaria29178perfumery
2aee682982e18eb4714ce9f97b15af5e28858442ea4d5dc5bb9e118e8f728095ddelivered2018-07-09 18:46:282018-07-11 03:45:452018-07-11 15:01:002018-07-12 18:14:352018-07-18 00:00:00boleto1...289cdb325fb7e7f891c38608bf9e09622018-07-13 03:45:45118.79.34belo horizonteSPperfumaria29178perfumery
3d543201a9b42a1402ff97e65b439a48b971bf8f42a9f8cb3ead257854905b454delivered2018-08-21 10:00:252018-08-21 10:50:542018-08-22 15:21:002018-08-28 18:58:222018-09-10 00:00:00credit_card2...289cdb325fb7e7f891c38608bf9e09622018-08-23 10:50:54116.922.75belo horizonteSPperfumaria29178perfumery
4d543201a9b42a1402ff97e65b439a48b971bf8f42a9f8cb3ead257854905b454delivered2018-08-21 10:00:252018-08-21 10:50:542018-08-22 15:21:002018-08-28 18:58:222018-09-10 00:00:00credit_card2...289cdb325fb7e7f891c38608bf9e09622018-08-23 10:50:54116.922.75belo horizonteSPperfumaria29178perfumery
\n", - "

5 rows × 26 columns

\n", - "
" - ], - "text/plain": [ - " order_id customer_id \\\n", - "0 53cdb2fc8bc7dce0b6741e2150273451 b0830fb4747a6c6d20dea0b8c802d7ef \n", - "1 86674ccaee19790309333210917b2c7d 1b338293f35549b5e480b9a3d7bbf3cd \n", - "2 aee682982e18eb4714ce9f97b15af5e2 8858442ea4d5dc5bb9e118e8f728095d \n", - "3 d543201a9b42a1402ff97e65b439a48b 971bf8f42a9f8cb3ead257854905b454 \n", - "4 d543201a9b42a1402ff97e65b439a48b 971bf8f42a9f8cb3ead257854905b454 \n", - "\n", - " order_status order_purchase_timestamp order_approved_at \\\n", - "0 delivered 2018-07-24 20:41:37 2018-07-26 03:24:27 \n", - "1 delivered 2018-08-09 11:37:35 2018-08-09 14:35:19 \n", - "2 delivered 2018-07-09 18:46:28 2018-07-11 03:45:45 \n", - "3 delivered 2018-08-21 10:00:25 2018-08-21 10:50:54 \n", - "4 delivered 2018-08-21 10:00:25 2018-08-21 10:50:54 \n", - "\n", - " order_delivered_carrier_date order_delivered_customer_date \\\n", - "0 2018-07-26 14:31:00 2018-08-07 15:27:45 \n", - "1 2018-08-10 14:34:00 2018-08-14 18:51:47 \n", - "2 2018-07-11 15:01:00 2018-07-12 18:14:35 \n", - "3 2018-08-22 15:21:00 2018-08-28 18:58:22 \n", - "4 2018-08-22 15:21:00 2018-08-28 18:58:22 \n", - "\n", - " order_estimated_delivery_date payment_type payment_installments ... \\\n", - "0 2018-08-13 00:00:00 boleto 1 ... \n", - "1 2018-08-22 00:00:00 credit_card 5 ... \n", - "2 2018-07-18 00:00:00 boleto 1 ... \n", - "3 2018-09-10 00:00:00 credit_card 2 ... \n", - "4 2018-09-10 00:00:00 credit_card 2 ... \n", - "\n", - " seller_id shipping_limit_date price freight_value \\\n", - "0 289cdb325fb7e7f891c38608bf9e0962 2018-07-30 03:24:27 118.7 22.76 \n", - "1 289cdb325fb7e7f891c38608bf9e0962 2018-08-13 14:31:29 116.9 18.92 \n", - "2 289cdb325fb7e7f891c38608bf9e0962 2018-07-13 03:45:45 118.7 9.34 \n", - "3 289cdb325fb7e7f891c38608bf9e0962 2018-08-23 10:50:54 116.9 22.75 \n", - "4 289cdb325fb7e7f891c38608bf9e0962 2018-08-23 10:50:54 116.9 22.75 \n", - "\n", - " seller_city seller_state product_category_name product_name_lenght \\\n", - "0 belo horizonte SP perfumaria 29 \n", - "1 belo horizonte SP perfumaria 29 \n", - "2 belo horizonte SP perfumaria 29 \n", - "3 belo horizonte SP perfumaria 29 \n", - "4 belo horizonte SP perfumaria 29 \n", - "\n", - " product_description_lenght product_category_name_english \n", - "0 178 perfumery \n", - "1 178 perfumery \n", - "2 178 perfumery \n", - "3 178 perfumery \n", - "4 178 perfumery \n", - "\n", - "[5 rows x 26 columns]" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "import pandas as pd\n", - "data = pd.read_csv('customer_segmentation.csv')\n", - "data.head()" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[('CustomersDatabase',), ('database_',), ('information_schema',), ('mockmidterm',), ('mysql',), ('performance_schema',), ('sys',)]\n", - "The database already exists! The old database has been deleted!)\n", - "Database is created\n" - ] - } - ], - "source": [ - "import mysql.connector as mysql\n", - "from mysql.connector import Error \n", - "\n", - "db_name='CustomersDatabase'\n", - "try:\n", - " mydb = mysql.connect(host='localhost', user='root', password='password123', auth_plugin='mysql_native_password') # you can add the auth_plugin here too (ref line 26)\n", - " if mydb.is_connected():\n", - " mycursor = mydb.cursor()\n", - " mycursor.execute('SHOW DATABASES')\n", - " result = mycursor.fetchall()\n", - " print(result)\n", - " for x in result:\n", - " if db_name == x[0]:\n", - " mycursor.execute('DROP DATABASE ' + db_name) # delete old database\n", - " mydb.commit() # make the changes official\n", - " print(\"The database already exists! The old database has been deleted!)\")\n", - " \n", - " mycursor.execute(\"CREATE DATABASE \"+ db_name)\n", - " print(\"Database is created\")\n", - "except Error as e:\n", - " print(\"Error while connecting to MySQL\", e)\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "141.46" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "dataset = pd.read_csv('customer_segmentation.csv')\n", - "dataset.head()\n", - "dataset['payment_value'][0]" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [], - "source": [ - "mycursor.execute(\"USE customersdatabase\")\n" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [], - "source": [ - "mycursor.execute(\n", - " ''' \n", - " CREATE TABLE customer(\n", - " customer_unique_id VARCHAR(40),\n", - " customer_id VARCHAR(40),\n", - " customer_city VARCHAR(40),\n", - " customer_state VARCHAR(40),\n", - " PRIMARY KEY (customer_unique_id)\n", - " );\n", - " '''\n", - " )" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": {}, - "outputs": [], - "source": [ - "mycursor.execute(\n", - " '''\n", - " CREATE TABLE seller(\n", - " seller_id VARCHAR(40),\n", - " seller_city VARCHAR(40),\n", - " seller_state VARCHAR(40),\n", - " PRIMARY KEY (seller_id)\n", - " );\n", - "\n", - "\n", - " '''\n", - "\n", - "\n", - "\n", - "\n", - "\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [], - "source": [ - "mycursor.execute(\n", - " '''\n", - " CREATE TABLE product(\n", - " product_id VARCHAR(40),\n", - " price int,\n", - " freight_value int,\n", - " product_category_name VARCHAR(60),\n", - " product_description_lenght int,\n", - " product_seller VARCHAR(40),\n", - " PRIMARY KEY (product_id),\n", - " FOREIGN KEY (product_seller) REFERENCES seller(seller_id)\n", - " );\n", - "\n", - "\n", - " '''\n", - "\n", - "\n", - "\n", - "\n", - "\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": {}, - "outputs": [], - "source": [ - "\n", - "\n", - "\n", - "\n", - "mycursor.execute(\n", - " '''\n", - " CREATE TABLE order_(\n", - " order_id VARCHAR(40),\n", - " order_status VARCHAR(40),\n", - " order_purchase_time TIMESTAMP,\n", - " order_approved_at TIMESTAMP,\n", - " order_delivered_carrier_date TIMESTAMP,\n", - " order_delivered_customer_date TIMESTAMP,\n", - " order_estimated_delivery_date TIMESTAMP,\n", - " payment_type VARCHAR(40),\n", - " payment_value int,\n", - " payment_installments int,\n", - " order_customer VARCHAR(40),\n", - " order_product VARCHAR(40),\n", - " order_seller VARCHAR(40),\n", - " PRIMARY KEY (order_id),\n", - " FOREIGN KEY (order_customer) REFERENCES customer(customer_unique_id),\n", - " FOREIGN KEY (order_seller) REFERENCES seller(seller_id),\n", - " FOREIGN KEY (order_product) REFERENCES product(product_id)\n", - " ); \n", - "\n", - " '''\n", - "\n", - "\n", - "\n", - "\n", - "\n", - ")\n" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3.9.13 ('base')", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.13" - }, - "orig_nbformat": 4, - "vscode": { - "interpreter": { - "hash": "2a92ed693cb77f20c14c67822919cbb2c31a455d141e9ef517a60f96c81b2b70" - } - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/README.md b/README.md index b0a9d44..a91c36e 100644 --- a/README.md +++ b/README.md @@ -1 +1,9 @@ Database project repository + +When you run the application, you will encounter 3 initial inputs. +-First will ask you to insert your path to enter the folder, it should be of type: C:/pathnametofolder (you don't need to add the "/" at the end of the path, we have already done it for you) +-Then it will ask you for your username, that is the username you use to enter the localhost for mysql +-Lastly it will ask you for the password to enter the localhost + +This is done so you can easily execute the application without having to edit the code itself. After doing this initial authentification, you will enter the application, create the database and populate the tables automatically. When the process is finished you will recieve an output line stating that the database has been created and the data has been loaded into the tables of the database. Afterwards you will recieve the 8 queries that you can choose to run. + diff --git a/program_database.py b/program_database.py new file mode 100644 index 0000000..77469eb --- /dev/null +++ b/program_database.py @@ -0,0 +1,535 @@ +def load_data(customer_database): + import pandas as pd + global file_path + file_path = input('Please insert the file path for the dataset: ') + data = pd.read_csv(file_path+"/customer_segmentation.csv") + import mysql.connector as mysql + from mysql.connector import Error + + global username + username = input("Please enter your mysql username: ") + global password + password = input("Please now enter your mysql password: ") + + db_name = "CustomersDatabase1" + try: + mydb = mysql.connect( + host="localhost", + user=username, + password=password, + auth_plugin="mysql_native_password", + ) + if mydb.is_connected(): + mycursor = mydb.cursor() + mycursor.execute("SHOW DATABASES") + result = mycursor.fetchall() + print(result) + for x in result: + if x[0] == 'customersdatabase1': + print("The database already exists!") + return None + + mycursor.execute("CREATE DATABASE CustomersDatabase1") + print("Database is created") + except Error as e: + print("Error while connecting to MySQL", e) + + mycursor.execute("USE CustomersDatabase1") + + mycursor.execute( + """ + CREATE TABLE customer( + customer_unique_id VARCHAR(40), + customer_id VARCHAR(40), + customer_city VARCHAR(40), + customer_state VARCHAR(40), + PRIMARY KEY (customer_unique_id) + ); + """ + ) + + mycursor.execute( + """ + CREATE TABLE seller( + seller_id VARCHAR(40), + seller_city VARCHAR(40), + seller_state VARCHAR(40), + PRIMARY KEY (seller_id) + ); + + + """ + ) + + mycursor.execute( + """ + CREATE TABLE product( + product_id VARCHAR(40), + price int, + freight_value int, + product_category_name VARCHAR(60), + product_description_lenght int, + PRIMARY KEY (product_id) + ); + + + """ + ) + + mycursor.execute( + """ + CREATE TABLE order_( + order_id VARCHAR(40), + order_status VARCHAR(40), + order_purchase_time DATETIME, + order_approved_at DATETIME, + order_delivered_carrier_date DATETIME, + order_delivered_customer_date DATETIME, + order_estimated_delivery_date DATETIME, + payment_type VARCHAR(40), + payment_value int, + payment_installments int, + order_customer VARCHAR(40), + order_product VARCHAR(40), + order_seller VARCHAR(40), + PRIMARY KEY (order_id), + FOREIGN KEY (order_customer) REFERENCES customer(customer_unique_id), + FOREIGN KEY (order_seller) REFERENCES seller(seller_id), + FOREIGN KEY (order_product) REFERENCES product(product_id) + ); + + """ + ) + mycursor.execute( + """ + CREATE TABLE offer( + id VARCHAR(40), + seller_id VARCHAR(40), + product_id VARCHAR(40), + PRIMARY KEY (id), + FOREIGN KEY (seller_id) REFERENCES seller(seller_id), + FOREIGN KEY (product_id) REFERENCES product(product_id) + ); + """ + ) + for i, row in data.iterrows(): + sql = "INSERT IGNORE INTO CustomersDatabase1.customer VALUES (%s,%s,%s,%s)" + mycursor.execute( + sql, + tuple( + [ + row["customer_unique_id"], + row["customer_id"], + row["customer_city"], + row["customer_state"], + ] + ), + ) + + mydb.commit() + for i, row in data.iterrows(): + sql = "INSERT IGNORE INTO CustomersDatabase1.seller VALUES (%s,%s,%s)" + mycursor.execute( + sql, tuple([row["seller_id"], row["seller_city"], row["seller_state"]]) + ) + + mydb.commit() + for i, row in data.iterrows(): + sql = "INSERT IGNORE INTO CustomersDatabase1.product VALUES (%s,%s,%s,%s,%s)" + mycursor.execute( + sql, + tuple( + [ + row["product_id"], + row["price"], + row["freight_value"], + row["product_category_name_english"], + row["product_description_lenght"], + ] + ), + ) + + mydb.commit() + + for i, row in data.iterrows(): + sql = "INSERT IGNORE INTO CustomersDatabase1.order_ VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" + mycursor.execute( + sql, + tuple( + [ + row["order_id"], + row["order_status"], + row["order_purchase_timestamp"], + row["order_approved_at"], + row["order_delivered_carrier_date"], + row["order_delivered_customer_date"], + row["order_estimated_delivery_date"], + row["payment_type"], + row["payment_value"], + row["payment_installments"], + row["customer_unique_id"], + row["product_id"], + row["seller_id"], + ] + ), + ) + + mydb.commit() + df2 = data.loc[:, ["product_id", "seller_id"]] + df2.drop_duplicates(inplace=True) + c = 0 + for i, row in df2.iterrows(): + sql = "INSERT IGNORE INTO CustomersDatabase1.offer VALUES (%s,%s,%s)" + mycursor.execute(sql, tuple([c, row["seller_id"], row["product_id"]])) + c += 1 + + mydb.commit() + + print("I loaded the dataset and built the database!\n") + + +def query1(): + import mysql.connector as mysql + from mysql.connector import Error + import matplotlib.pyplot as plt + import pandas as pd + + mydb = mysql.connect( + host="localhost", + user=username, + password=password, + auth_plugin="mysql_native_password", + ) + mycursor = mydb.cursor() + mycursor.execute("USE CustomersDatabase1") + sql = mycursor.execute( + """ + select product_category_name,count(product_category_name) from product + GROUP BY + product_category_name + ORDER BY count(product_category_name) desc + """ + ) + mycursor.execute(sql) + result = mycursor.fetchall() + df=pd.DataFrame(result) + df.columns=["Categories","Occurencies"] + print(df) + df.plot(x="Categories", y="Occurencies", kind='bar') + plt.xlabel("Categories") + plt.ylabel('Number of occurencies') + plt.title('Frequence of different Categories') + for element in result: + print(element) + + +def query2(): + import mysql.connector as mysql + from mysql.connector import Error + import matplotlib.pyplot as plt + import pandas as pd + + mydb = mysql.connect( + host="localhost", + user=username, + password=password, + auth_plugin="mysql_native_password", + ) + mycursor = mydb.cursor() + mycursor.execute("USE CustomersDatabase1") + sql = mycursor.execute( + """ + select count(c.customer_city), c.customer_city from customer as c, order_ as o + where c.customer_unique_id = o.order_customer + group by c.customer_city + order by count(c.customer_city) DESC; + """ + ) + mycursor.execute(sql) + result = mycursor.fetchall() + df=pd.DataFrame(result) + df.columns=["Occurencies","Cities"] + df.plot(x="Cities", y="Occurencies", kind='bar') + plt.xlabel("Cities") + plt.ylabel('Number of occurencies') + plt.title('Frequence of different Cities') + for element in result: + print(element) + + +def query3(): + import mysql.connector as mysql + from mysql.connector import Error + import matplotlib.pyplot as plt + import pandas as pd + + mydb = mysql.connect( + host="localhost", + user=username, + password=password, + auth_plugin="mysql_native_password", + ) + mycursor = mydb.cursor() + mycursor.execute("USE CustomersDatabase1") + sql = mycursor.execute( + """ + select count(s.seller_id),s.seller_id from seller as s, order_ as o + where s.seller_id = o.order_seller + group by s.seller_id + order by count(s.seller_id) DESC; + """ + ) + mycursor.execute(sql) + result = mycursor.fetchall() + df=pd.DataFrame(result) + df.columns=["Occurencies","Seller"] + print(df) + df.plot(x="Seller" , kind='Occurencies') + plt.xlabel("Sellers") + plt.ylabel('Number of occurencies') + plt.title('Frequency of different Sellers') + for element in result: + print(element) + + +def query4(): + import mysql.connector as mysql + from mysql.connector import Error + import matplotlib.pyplot as plt + import pandas as pd + + mydb = mysql.connect( + host="localhost", + user=username, + password=password, + auth_plugin="mysql_native_password", + ) + mycursor = mydb.cursor() + mycursor.execute("USE CustomersDatabase1") + sql = mycursor.execute( + """ + select avg(o.payment_installments) from order_ as o; + + """ + ) + mycursor.execute(sql) + result = mycursor.fetchall() + for element in result: + print(element) + + +def query5(): + import mysql.connector as mysql + from mysql.connector import Error + import matplotlib.pyplot as plt + + mydb = mysql.connect( + host="localhost", + user=username, + password=password, + auth_plugin="mysql_native_password", + ) + mycursor = mydb.cursor() + mycursor.execute("USE CustomersDatabase1") + sql = mycursor.execute( + """ + select c.customer_id, o.payment_value, o.payment_installments, o.payment_type from order_ as o, customer as c + where c.customer_unique_id = o.order_customer + group by c.customer_id, o.payment_installments, o.payment_value,o.payment_type; + """ + ) + mycursor.execute(sql) + result = mycursor.fetchall() + for element in result: + print(element) + + +def query6(): + import mysql.connector as mysql + from mysql.connector import Error + import pandas as pd + + data = pd.read_csv(file_path+"/customer_segmentation.csv") + possible_cities = [] + possible_payment_types = [] + yes_no = input('Do you want to see the list of all possible cities? ').lower() + if yes_no == 'yes': + for city in data["customer_city"]: + if city not in possible_cities: + possible_cities.append(city) + print("Possible cities to select: ", possible_cities) + for payments in data["payment_type"]: + if payments not in possible_payment_types: + possible_payment_types.append(payments) + print("") + print("Possible payment types to select: ", possible_payment_types) + mydb = mysql.connect( + host="localhost", + user=username, + password=password, + auth_plugin="mysql_native_password", + ) + mycursor = mydb.cursor() + mycursor.execute("USE CustomersDatabase1") + + city = input("Please insert the desired city: ") + payment_type = input("Please insert the desired payment_type: ") + sql = mycursor.execute( + """ + select count(c.customer_city), c.customer_city, o.payment_type from customer as c, order_ as o + where c.customer_unique_id = o.order_customer and c.customer_city = %s and o.payment_type = %s; + + """, + (city, payment_type), + ) + mycursor.execute(sql) + result = mycursor.fetchall() + if len(result) == 0: + print('There are no results that satisty your input: ',city , amount) + for element in result: + print(element) + + +def query7(): + import mysql.connector as mysql + from mysql.connector import Error + import pandas as pd + + data = pd.read_csv(file_path+"/customer_segmentation.csv") + possible_types = [] + for types in data["payment_type"]: + if types not in possible_types: + possible_types.append(types) + print("Possible payment types to select: ", possible_types) + + payment_type = input("please enter the desired payment type: ") + mydb = mysql.connect( + host="localhost", + user=username, + password=password, + auth_plugin="mysql_native_password", + ) + mycursor = mydb.cursor() + mycursor.execute("USE CustomersDatabase1") + sql = mycursor.execute( + """ + select avg(o.payment_value), o.payment_type from order_ as o + where o.payment_type = %s; + """, + (payment_type,) + ) + mycursor.execute(sql) + result = mycursor.fetchall() + if len(result) == 0: + print('There is no payment type that you wanted: ',payment_type) + for element in result: + print(element) + +def query8(): + import mysql.connector as mysql + from mysql.connector import Error + import pandas as pd + + data = pd.read_csv(file_path+"/customer_segmentation.csv") + possible_cities = [] + yes_no = input('Do you want to see the list of all possible cities? ').lower() + if yes_no == 'yes': + for city in data["customer_city"]: + if city not in possible_cities: + possible_cities.append(city) + print("Possible cities to select: ", possible_cities) + + amount = int(input("please enter minimum amount: ")) + city = input("please enter the desired city: ") + possible_types = [] + for types in data["payment_type"]: + if types not in possible_types: + possible_types.append(types) + print("Possible payment types to select: ", possible_types) + payment_type = input('please enter the desired payment type: ') + mydb = mysql.connect( + host="localhost", + user=username, + password=password, + auth_plugin="mysql_native_password", + ) + mycursor = mydb.cursor() + mycursor.execute("USE CustomersDatabase1") + sql = mycursor.execute( + """ + select count(c.customer_id), sum(o.payment_value) as x, c.customer_city, o.payment_type from customer as c, order_ as o + where c.customer_unique_id = o.order_customer and c.customer_city = %s and o.payment_type = %s + having x > %s; + """, + (city,payment_type, amount,), + ) + mycursor.execute(sql) + result = mycursor.fetchall() + if len(result) == 0: + print('There are no orders in this city with amount: ',amount) + return None + for element in result: + print(element) + + +if __name__ == "__main__": + print("Welcome to our project!\n") + load_data("customer_segmentation.csv") + + valid_choices = [ "Query to show the number of orders for each product: ","1","Query to show the number of orders for each city: ", "2", "Query to show the number of orders sold by sellers: ","3","Query to show the avarage number of installments: ", "4","Query to show the avarage number of installments: ", "5","Query to show the number of orders for your desired city and payment type: ", "6","query will show the average payment value for the desired payment type: ", "7","Query to show the number of customers that have spent at least the desired amount and living in the determined city: ", "8", "quit"] + + while True: + print("possible choices: \n") + for choices in valid_choices: + print(choices) + choice = input("""\n\nPlease choose a query to run!\n""").lower() + + if choice not in valid_choices: + print(f"Your choice '{choice}' is not valid. Please insert a valid option") + continue + if choice == "quit": + break + print(f"\nYou chose to execute query {choice}") + if choice == "1": + print("This query will show the number of orders for each product \n ") + query1() + continue + elif choice == "2": + print("This query will show the number of orders for each city \n") + query2() + continue + elif choice == "3": + print("This query will show the number of orders sold by each seller \n") + query3() + continue + elif choice == "4": + print("This query will show the avarage number of installments \n") + query4() + continue + elif choice == "5": + print( + "This query will show payment value, payment type and installments for each order \n" + ) + query5() + continue + elif choice == "6": + print( + "This query will show the number of orders for your desired city and payment type \n" + ) + query6() + continue + elif choice == "7": + print( + "This query will show the average payment value for the desired payment type \n" + ) + query7() + continue + elif choice == "8": + print( + "This query will show the number of customers that have spent at least the desired amount, are living in a determined city and have used a specific payment type \n" + ) + query8() + continue + else: + raise Exception("Something went really wrong") + print("\nGoodbye!\n")