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

Append an Item to a List in Python

To append an item at the end of a list: Copy list_name.append(“new item”) To add multiple items to a list: Copy list_name.extend([“item_1”, “item_2”, “item_3”, …]) Steps to Append an Item to a List in Python Step 1: Create a List To start, create a list in Python. For instance: Copy product_list = [“Oven”, “Toaster”, “Dishwasher”, … Read more

How to Rename Columns in Pandas DataFrame

To rename a single column in Pandas DataFrame: Copy df.rename(columns={‘old column name’: ‘new column name’}, inplace=True) To rename multiple columns in Pandas DataFrame: Copy df.rename(columns={‘old column 1’: ‘new column 1’, ‘old column 2’: ‘new column 2′}, inplace=True) Example 1: Rename a Single Column in Pandas DataFrame Let’s say that you created a DataFrame in Python, … Read more

How to Reset an Index in Pandas DataFrame

To reset an index in Pandas DataFrame: Copy df.reset_index(drop=True, inplace=True) Steps to Reset an Index in Pandas DataFrame Step 1: Gather your data For illustration purposes, let’s gather the following data about different products: Product Price Tablet 250 Printer 150 Laptop 1200 Monitor 300 Computer 1500 Step 2: Create a DataFrame Next, create a DataFrame … Read more

Convert a List to Pandas Dataframe (with examples)

At times, you may need to convert a list to Pandas DataFrame in Python. You may then use the following template to convert your list to a DataFrame: Copy import pandas as pd list_name = [‘item_1’, ‘item_2’, ‘item_3’, …] df = pd.DataFrame(list_name, columns=[‘column_name’]) In the next section, you’ll see how to perform the conversion in … Read more

How to Create a List in Python

To create a list in Python: Copy list_name = [‘item1’, ‘item2’, ‘item3’, ….] Examples Here are examples of two lists in Python: (1) List of products – this list contains strings (by placing the values within quotes): Copy products = [“microwave”, “oven”, “toaster”, “refrigerator”, “dishwasher”] (2) List of prices – this list contains numbers (i.e., … Read more