Screenshot to PDF using Python (example included)

In this tutorial, you’ll see the complete steps to take a screenshot and then convert it to PDF using Python. To accomplish this goal you’ll need to use the following packages: pyautogui pillow (pil) Steps to take a screenshot and then convert it to PDF using Python Step 1: Install the relevant packages If you … Read more

How to Sort an Index in Pandas DataFrame

You can sort an index in Pandas DataFrame: (1) In an ascending order: Copy df = df.sort_index() (2) In a descending order: Copy df = df.sort_index(ascending=False) Let’s see how to sort an index by reviewing an example. The Example To start, let’s create a simple DataFrame: Copy import pandas as pd data = {‘Product’: [‘Computer’,’Printer’,’Monitor’,’Desk’,’Phone’,’Tablet’,’Scanner’], … Read more

How to Drop Rows by Index in Pandas DataFrame

Here are two ways to drop rows by the index in Pandas DataFrame: (1) Drop single row by index. For example, you may use the syntax below to drop the row that has an index of 2: Copy df = df.drop(index=2)  (2) Drop multiple rows by index. For instance, to drop the rows with the … Read more

How to Export SQL Server Table to CSV using Python

In this guide, you’ll see the complete steps to export SQL Server table to a CSV file using Python. The Example Let’s say that you’d like 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 … 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

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: Write a string to a text file Overwrite the original string Display a list of strings … Read more

How to Randomly Select Columns from Pandas DataFrame

Depending on your needs, you may use either of the 4 techniques below in order to randomly select columns from 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 … 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

4 Ways to 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

How to Convert Strings to Integers in a Python List

You may apply either of the two approaches below in order to convert strings to integers in a Python list: (1) Using map Copy my_list = [‘value1′,’value2′,’value3’,…] my_list = list(map(int, my_list)) (2) Using list comprehension Copy my_list = [‘value1′,’value2′,’value3′,…] my_list = [int(i) for i in my_list] Let’s now review several examples. Using Map to Convert … Read more