Convert a Dictionary to Pandas DataFrame

You may use the following template to convert a dictionary to Pandas DataFrame: Copy import pandas as pd my_dict = {‘key_1’: ‘value_1’, ‘key_2’: ‘value_2’, …} df = pd.DataFrame(list(my_dict.items()), columns=[‘column_1’, ‘column_2’]) print(df) Steps to Convert a Dictionary to Pandas DataFrame Step 1: Gather the Data for the Dictionary To start, gather the data for your dictionary. … Read more

Average each Column and Row in Pandas DataFrame

To get the average of each numeric column and row in Pandas DataFrame (apply only for numeric values): Average each column: Copy df.mean(axis=0) Average each row: Copy df.mean(axis=1) Steps to get the Average of each Column and Row in Pandas DataFrame Step 1: Prepare the data For example, here is a simple dataset that contains … Read more

How to Create a Correlation Matrix using Pandas

To create a correlation matrix using Pandas: Copy df.corr() Steps to Create a Correlation Matrix Step 1: Collect the Data Firstly, collect the data for the correlation matrix. For example, here is a dataset that contains 3 variables: A B C 45 38 10 37 31 15 42 26 17 35 28 21 39 33 … Read more

Create a Pie Chart in Python using Matplotlib

To create a pie chart in Python using Matplotlib: Copy import matplotlib.pyplot as plt my_data = [value1, value2, value3, …] my_labels = [“label1”, “label2”, “label3″, …] plt.pie(my_data, labels=my_labels, autopct=”%1.1f%%”) plt.title(“My Title”) plt.axis(“equal”) plt.show() Steps to Create a Pie Chart in Python using Matplotlib Step 1: Install the Matplotlib package Install the Matplotlib package if you … Read more

Convert Integers to Floats in Pandas DataFrame

Two approaches to convert integers to floats in Pandas DataFrame: (1) The astype(float) approach: Copy df[‘DataFrame Column’] = df[‘DataFrame Column’].astype(float) (2) The apply(float) approach: Copy df[‘DataFrame Column’] = df[‘DataFrame Column’].apply(float) Steps to Convert Integers to Floats in Pandas DataFrame Step 1: Create a DataFrame To start, create a DataFrame that contains integers. For example, let’s … Read more

Convert CSV to Excel using Python

To convert a CSV file to an Excel file using Python: Copy import pandas as pd read_file = pd.read_csv(r”Path where the CSV file is stored\File name.csv”) read_file.to_excel(r”Path to store the Excel file\File name.xlsx”, index=False, header=True) Steps to Convert a CSV to Excel using Python Step 1: Install the Pandas package If you haven’t already done … Read more

How to Take a Screenshot using Python

You may use the following template to take a screenshot using Python: Copy import pyautoguimy_screenshot = pyautogui.screenshot()my_screenshot.save(r”Path to save screenshot\file name.png”) Steps to Take a Screenshot using Python Step 1: Install the pyautogui package To start, install the pyautogui package using this command: Copy pip install pyautogui You may check the following guide for the … Read more

Pandas DataFrame to SQL

In this short guide, you’ll see how to get from Pandas DataFrame to SQL. Steps Step 1: Create a DataFrame To start, create a DataFrame with the following data about products: Copy import pandas as pddata = { “product_name”: [“Computer”, “Tablet”, “Monitor”, “Printer”], “price”: [900, 300, 450, 150],}df = pd.DataFrame(data)print(df) Run the code, and you’ll get … Read more

List all Text Files in a Directory using Python

To list all text files in a directory using Python: List all the text files in a directory: Copy import globimport osos.chdir(r”directory where the files are located”)my_files = glob.glob(“*.txt”)print(my_files) List the paths of the text files: Copy import globmy_files_path = glob.glob(r”directory where the files are located\*.txt”)print(my_files_path) Steps to List all Text Files in a Directory … Read more

Rename a File using Python

The os module can be used to rename a file using Python: Copy import os os.rename(r”Directory path\OLD_file_name.file_type”, r”Directory path\NEW_file_name.file_type”) Steps to Rename a File using Python Suppose that your goal is to rename a text file from “cars” to “trucks“ Here are the steps that you may follow to rename your file: Step 1: Capture … Read more