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

Convert Strings to Integers in a Python List

Here are 3 approaches to convert strings to integers in a Python list: (1) Using a map function: Copy list_of_strings = [“value1”, “value2”, “value3”, …]list_of_integers = list(map(int, list_of_strings))print(list_of_integers) For example: Copy list_of_strings = [“22”, “45”, “75”, “68”, “39”]list_of_integers = list(map(int, list_of_strings))print(list_of_integers) The result: (2) Using a List Comprehension: Copy list_of_strings = [“value1”, “value2”, “value3”, …]list_of_integers … Read more

Add Suffix to Each Column Name in Pandas DataFrame

To add a suffix to each column name in Pandas DataFrame: Copy df = df.add_suffix(‘your suffix’) Steps to Add Suffix to Each Column Name in Pandas DataFrame Step 1: Create a DataFrame To start with a simple example, let’s say that you have the following dataset that contains 3 columns: Product Price Discount ABC 750 … Read more

How to Concatenate Two Lists in Python

To concatenate two lists in Python: (1) Using the + operator: Copy list_one = [“item1”, “item2”, “item3”, …]list_two = [“item1”, “item2”, “item3”, …]concatenated_list = list_one + list_twoprint(concatenated_list) (2) Using extend: Copy list_one = [“item1”, “item2”, “item3”, …]list_two = [“item1”, “item2”, “item3”, …]list_one.extend(list_two)print(list_one) The Steps Step 1: Create two Lists To begin with a simple example, … Read more

Generate Random Numbers in a Python List

Here are 2 ways to generate random numbers in a Python list: (1) Generate random numbers that cannot be repeated: Copy import randommy_list = random.sample(range(lowest number, highest number), number of items in the list)print(my_list) For example, let’s generate 15 random numbers, where the lowest number is 1 (inclusive), while the highest number is 30 (exclusive): … Read more

Add Prefix to Each Column Name in Pandas DataFrame

To add a prefix to each column name in Pandas DataFrame: Copy df = df.add_prefix(‘my_prefix’) Steps to Add Prefix to Each Column Name in Pandas DataFrame Step 1: Create a DataFrame To start with a simple example, let’s suppose that you have the following dataset with 3 columns: Product Price Discount ABC 750 Yes DDD … Read more

Check the Scikit-Learn Version Installed

You can check the Scikit-Learn version installed using this syntax: Copy import sklearn print(sklearn.__version__) Run the code in Python, and you’ll get the version of Scikit-Learn. Here is an example of the version installed that you may get: Check the version installed using the command line You can also check the Scikit-Learn version installed using … Read more

Change Strings to Lowercase in Pandas DataFrame

To change strings to lowercase in Pandas DataFrame: Copy df[‘column name’] = df[‘column name’].str.lower() Steps to Change Strings to Lowercase in Pandas DataFrame Step 1: Create a DataFrame To begin, create a simple DataFrame with 5 fruits (all in uppercase) and their prices: Copy import pandas as pd data = {‘Fruits’: [‘BANANA’, ‘APPLE’, ‘MANGO’, ‘WATERMELON’, … Read more

Create a Bar Chart in Python using Matplotlib

To create a bar chart in Python using Matplotlib: Copy import matplotlib.pyplot as pltx_axis = [“value_1”, “value_2”, “value_3”, …]y_axis = [“value_1”, “value_2”, “value_3”, …]plt.bar(x_axis, y_axis)plt.title(“title name”)plt.xlabel(“x_axis name”)plt.ylabel(“y_axis name”)plt.show() Steps to Create a Bar Chart in Python using Matplotlib Step 1: Install the Matplotlib package If you haven’t already done so, install the Matplotlib package using … Read more

Select Rows Containing a Substring in Pandas DataFrame

In this short guide, you’ll see how to select rows that contain a specific substring in Pandas DataFrame. In particular, you’ll observe 5 cases to get all rows that: The Example To start, create a DataFrame in Python with the following data: Copy import pandas as pddata = { “month”: [“January”, “February”, “March”, “April”, “May”, … Read more