Add a Column to an Existing Table using ALTER TABLE

Here is the syntax to add a column to an existing table using ALTER TABLE: Copy ALTER TABLE table_name ADD new_column_name data_type Next, you’ll see how to apply the above syntax using a simple example. Add a Column to an Existing Table using ALTER TABLE Suppose that you have a table called products. The ‘products’ table … Read more

How to Apply UNION and UNION ALL using SQL

You can use the following syntax in order to apply UNION and UNION ALL using SQL: (1) Apply UNION while removing duplicates: Copy SELECT column_1, column_2,… FROM table_1 UNION SELECT column_1, column_2,… FROM table_2 (2) Apply UNION ALL while keeping the duplicates: Copy SELECT column_1, column_2,… FROM table_1 UNION ALL SELECT column_1, column_2,… FROM table_2 … Read more

How to to Replace Values in a DataFrame in R

Here is the syntax to replace values in a DataFrame in R: (1) Replace a value across the entire DataFrame: Copy df[df == “Old Value”] <- “New Value” (2) Replace a value under a single DataFrame column: Copy df[“Column Name”][df[“Column Name”] == “Old Value”] <- “New Value” Next, you’ll see 4 scenarios that describe how … Read more

Categories R

Sort Pandas Series

Here is the syntax to sort Pandas Series: (1) Sort Pandas Series in an ascending order: Copy ser.sort_values(ascending=True, inplace=True) (2) Sort Pandas Series in a descending order. In this case, simply set ascending=False: Copy ser.sort_values(ascending=False, inplace=True) In this guide, you’ll see how to sort Pandas Series that contains: Sort Pandas Series that Contains String/Text Values … Read more

How to Get the File Size using Python

To get the file size (in bytes) using Python: Copy import os file_path = r”path where the file is stored\file_name.file_extension” file_size = os.path.getsize(file_path) print(file_size) Optionally, to get the size in bytes, kilobytes, megabytes and gigabytes: Copy import os file_path = r”path where the file is stored\file_name.file_extension” file_size = os.path.getsize(file_path) print(“File size in Bytes: ” + … Read more

How to Extract the File Extension using Python

Here are 3 ways to extract the file extension using Python: (1) Extract the file extension with the dot: Copy import os.path file_path = r”path where the file is stored\file_name.file_extension” file_extension = os.path.splitext(file_path)[1] print(file_extension) (2) Extract the file extension without the dot: Copy import os.path file_path = r”path where the file is stored\file_name.file_extension” file_extension = … Read more

Get the first N rows in Pandas DataFrame

You can use df.head() to get the first N rows in Pandas DataFrame. For example, if you need the first 4 rows, then use: Copy df.head(4) Alternatively, you can specify a negative number within the brackets to get all the rows, excluding the last N rows. For example, you can use the following syntax to … Read more

Get the Latest File in a Folder using Python

To get the latest file in a folder using Python (where “type” represents the file type, such as csv, or txt): Copy import globimport os.pathfolder_path = r”path where your files are located”file_type = r”\*type”files = glob.glob(folder_path + file_type)max_file = max(files, key=os.path.getctime)print(max_file) Steps to get the Latest File in a Folder using Python Step 1: Capture … Read more

Convert JSON to TEXT File using Python

The following template can be used to convert a JSON file to a text file using Python: Copy import pandas as pddf = pd.read_json(r”Path where the JSON file is saved\File Name.json”)df.to_csv(r”Path where the new TEXT file will be stored\New File Name.txt”, index=False) Steps to Convert JSON to TEXT using Python Step 1: Prepare the JSON … Read more

How to Get the Modified Time of a File using Python

You can use the following template to get the modified time of a file using Python: Copy import os.path modified_time = os.path.getmtime(r’path where the file is stored\file name.file extension’) print(modified_time) The above approach expresses the time in seconds since the epoch. If you wish to convert the time to a string that represents a local … Read more