How to Move a File or Directory in Python

To move a file in Python: Copy import shutil original = r”original path of the file\file_name.file_extension” target = r”target path to store the file\file_name.file_extension” shutil.move(original, target) Steps to Move a File in Python Step 1: Capture the Original Path To begin, capture the original path where your file is currently stored. For example, let’s suppose … Read more

Convert PNG to JPG using Python

To convert PNG to JPG using Python: Copy from PIL import Imageim = Image.open(r”path where the PNG is stored\file name.png”)im.save(r”path where the JPG will be stored\new file name.jpg”) Steps to Convert PNG to JPG using Python Step 1: Install the PIL package You can install the PIL package using the command below: Copy pip install … Read more

How to Sort a List in Python

To sort a List in Python in an ascending order: Copy my_list.sort() To sort a List in a descending order: Copy my_list.sort(reverse=True) 4 Cases of Sorting a List Case 1: Sort a List in an Ascending Order To start, create a List of names: Copy names = [“Jon”, “Maria”, “Bill”, “Liam”, “Emma”]print(names) The resulted List: … Read more

How to Copy a File using Python

In this short guide, you’ll see how to copy a file, from one folder to another, using Python. To start, here is a simple template that you may use to copy a file in Python using shutil.copyfile: Copy import shutil source = r”source path where the file is currently stored\file_name.file_extension” target = r”target path where … Read more

Convert JPG to PNG using Python

To convert JPG to PNG using Python: Copy from PIL import Imageim = Image.open(r”path where the JPG is stored\file name.jpg”)im.save(r”path where the PNG will be stored\new file name.png”) Steps to Convert JPG to PNG using Python Step 1: Install the PIL Package To start, install the PIL package using the command below: Copy pip install … Read more

Convert Integers to Strings in Pandas DataFrame

Here are 2 ways to convert integers to strings in Pandas DataFrame: (1) Using apply(str): Copy df[“DataFrame Column”] = df[“DataFrame Column”].apply(str) (2) Using astype(str): Copy df[“DataFrame Column”] = df[“DataFrame Column”].astype(str) Steps to Convert Integers to Strings in Pandas DataFrame Step 1: Create a DataFrame with Integers To start, create a DataFrame with integers. For example, … Read more

How to Check the Data Type in Pandas DataFrame

To check the data type of all columns in Pandas DataFrame: Copy df.dtypes To check the data type of a particular column in Pandas DataFrame: Copy df[‘DataFrame Column’].dtypes Steps to Check the Data Type in Pandas DataFrame Step 1: Create a DataFrame To start, create a DataFrame with 3 columns: Copy import pandas as pd … Read more

Convert JSON String to CSV using Python

In this short guide, you’ll see the steps to convert a JSON string to CSV using Python. To begin, you may use the following template to perform the conversion: Copy import pandas as pd df = pd.read_json(r”Path where the JSON file is saved\File Name.json”) df.to_csv(r”Path where the new CSV file will be stored\New File Name.csv”, … Read more

Convert Excel to CSV using Python

To convert an Excel file to a CSV file using Python: Copy import pandas as pd read_file = pd.read_excel(r”Path where the Excel file is stored\File name.xlsx”) read_file.to_csv(r”Path to store the CSV file\File name.csv”, index=False, header=True) And if you have a specific Excel sheet that you’d like to convert, you may then use this template: Copy … Read more