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

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

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

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