LEFT JOIN using SQL

You can use a LEFT JOIN to get all the records from the left table, and the records from the right table where there is a match (if there is no match, you’ll get NULL under the selected fields from the right table): Copy SELECT tb1.*,tb2.*FROM left_table_name tb1LEFT JOIN right_table_name tb2 ON tb1.id = tb2.id … Read more

Export SQL Server Table to CSV using Python

In this guide, you’ll see the full steps to export SQL Server table to a CSV file using Python. The Example Assume that you want to export the following table (called the ‘dbo.product‘ table) from SQL Server to CSV using Python: product_id product_name price 1 Computer 800 2 TV 1200 3 Printer 150 4 Desk … Read more

Drop Columns from Pandas DataFrame

Here are two approaches to drop columns from Pandas DataFrame (1) Drop a single column from the DataFrame: Copy df.drop(‘column name’, axis=1, inplace=True) (2) Drop multiple columns from the DataFrame: Copy df.drop([‘column 1’, ‘column 2’, ‘column 3′, …], axis=1, inplace=True) The Example To start with a simple example, let’s create a DataFrame with 5 columns: … Read more

Update Table Records using SQL

To update records using SQL: Copy UPDATE table_nameSET column_1 = value_1, column_2 = value_2, …WHERE condition The Example Suppose that you created a table, where: The ‘product’ table also contains the following 6 records: product_id product_name price 1 Computer 800 2 TV 1200 3 Printer 150 4 Desk 400 5 Chair 120 6 Tablet 300 … Read more

How to Write a String to a Text File using Python

To write a string to a text file using Python: Copy text_file = open(r”path where the text file will be created\file name.txt”, “w”)my_string = “type your string here”text_file.write(my_string)text_file.close() In this short guide, you’ll see how to: Steps to Write a String to a Text File using Python Step 1: Specify the path for the text … Read more

Delete Records from a Table using SQL

Here are two ways to delete records from a table using SQL: (1) Delete records based on specified conditions: Copy DELETE FROM table_nameWHERE condition (2) Delete all the records in a given table: Copy DELETE FROM table_name The Example Suppose that you created a table, where: The ‘product’ table also contains the following 6 records: … Read more

Insert Records Into a Table using SQL

To insert records into a table using SQL: Copy INSERT INTO table_name (column_1, column_2, column_3,…)VALUES(‘value_1’, ‘value_2’, ‘value_3’, …) The Example Assume that you created an empty table called the ‘product‘ table which contains 3 columns: product_id, product_name, and price: Copy CREATE TABLE product ( product_id int primary key, product_name nvarchar(50), price int) The ultimate goal is … Read more

Randomly Select Columns in Pandas DataFrame

Here are 4 ways to randomly select columns in Pandas DataFrame: (1) Randomly select a single column: Copy df = df.sample(axis=”columns”) (2) Randomly select a specified number of columns. For example, to select 3 random columns, set n=3: Copy df = df.sample(n=3, axis=”columns”)  (3) Allow a random selection of the same column more than once … Read more

Change Strings to Uppercase in Pandas DataFrame

To change strings to uppercase in Pandas DataFrame: Copy df[‘column name’] = df[‘column name’].str.upper() Steps to Change Strings to Uppercase in Pandas DataFrame Step 1: Create a DataFrame To start, let’s create a DataFrame with 5 vegetables (all in lowercase) and their prices: Copy import pandas as pd data = {‘Vegetables’: [‘broccoli’, ‘carrot’, ‘onion’, ‘celery’, … Read more

Randomly Select Rows from Pandas DataFrame

Here are 4 ways to randomly select rows from Pandas DataFrame: (1) Randomly select a single row: Copy df = df.sample() (2) Randomly select a specified number of rows. For example, to select 3 random rows, set n=3: Copy df = df.sample(n=3) (3) Allow a random selection of the same row more than once (by … Read more