How to Apply SQL in Python using sqlite3

In this short guide, you’ll see how to apply SQL in Python using sqlite3 by: Steps to Apply SQL in Python using sqlite3 Step 1: Create a database and tables using sqlite3 In this step, you’ll see how to create: Where the ‘items‘ table would contain the following columns and data: item_id item_name 1 Microwave … Read more

Linear Regression in Python using Statsmodels

In this short guide, you’ll see how to perform a linear regression in Python using statsmodels. Here are the topics to be reviewed: Background Linear regression models assume a linear relationship between the dependent variable (which is the variable you are trying to predict/estimate) and the independent variable/s (the input variable/s used in the prediction). Under a … Read more

Get the Current, Previous and Next-Day Dates in Python

To get the system dates with the timestamps in Python (check case 2 below to get the dates without the timestamps): Current Date: Copy import datetimecurrent_date = datetime.datetime.today()print(current_date) Previous Date: Copy import datetimeprevious_date = datetime.datetime.today() – datetime.timedelta(days=1)print(previous_date) Next-Day Date: Copy import datetimenext_day_date = datetime.datetime.today() + datetime.timedelta(days=1)print(next_day_date) Case 1: System Dates with the Timestamps Here is … Read more

Create a Database in Python using sqlite3

In this guide, you’ll see how to create a database in Python using sqlite3, including the steps to: The Steps Step 1: Create the Database and Tables In this step, you’ll see how to create: Where the columns to be added to the 2 tables are: Table Name Column Name Column Format products product_id Integer … Read more

How to Import a CSV File into Python using Pandas

To import a CSV file into Python using Pandas: Copy import pandas as pd df = pd.read_csv(r”Path where the CSV file is stored\file_name.csv”) print(df) The Example To begin with a simple example, suppose that you have the following data stored in a CSV file (where the file name is “my_products“): product brand price Computer A … Read more

How to Connect Python to SQL Server using pyodbc

To connect Python to SQL Server using pyodbc: Copy import pyodbcconn = pyodbc.connect( “Driver={SQL Server};” “Server=server_name;” “Database=database_name;” “Trusted_Connection=yes;”)cursor = conn.cursor()cursor.execute(“SELECT * FROM table_name”)for i in cursor: print(i) The Example Let’s review a simple example, where: product_id product_name price 1 Laptop 1100 2 Printer 200 3 Keyboard 80 4 Monitor 450 5 Tablet 300 Steps to Connect … Read more

Connect Python to Oracle Database using cx_Oracle

To connect Python to an Oracle database using cx_Oracle: Copy import cx_Oracledsn_tns = cx_Oracle.makedsn(“Host Name”, “Port Number”, service_name=”Service Name”)connection = cx_Oracle.connect( user=r”User Name”, password=”Personal Password”, dsn=dsn_tns)cursor = connection.cursor()cursor.execute(“SELECT * FROM database.table”)for row in cursor: print(row)cursor.close()connection.close() Steps to Connect Python to Oracle using cx_Oracle Step 1: Install the cx_Oracle package To start, install the cx_Oracle package … Read more