Convert Floats to Integers in Pandas DataFrame

You can convert floats to integers in Pandas DataFrame using: (1) astype(int): Copy df[“DataFrame Column”] = df[“DataFrame Column”].astype(int) (2) apply(int): Copy df[“DataFrame Column”] = df[“DataFrame Column”].apply(int) In this guide, you’ll see 4 scenarios of converting floats to integers for: 4 Scenarios of Converting Floats to Integers in Pandas DataFrame (1) Convert floats to integers for … Read more

Convert Pandas DataFrame to a Dictionary

To convert Pandas DataFrame to a dictionary: Copy my_dictionary = df.to_dict() Steps to Convert Pandas DataFrame to a Dictionary Step 1: Create a DataFrame For example, create a DataFrame with two columns: Copy import pandas as pddata = { “Product”: [“Laptop”, “Printer”, “Monitor”, “Tablet”], “Price”: [1200, 100, 300, 150],}df = pd.DataFrame(data)print(df)print(type(df)) You’ll then get the … Read more

How to Convert Pandas DataFrame to Series

To convert Pandas DataFrame to Series using squeeze: Copy df.squeeze() In this guide, you’ll see 3 scenarios of converting: (1) Convert a Single DataFrame Column into a Series To start with a simple example, let’s create a DataFrame with a single column: Copy import pandas as pd data = {“Products”: [“Computer”, “Printer”, “Tablet”, “Chair”, “Desk”]} … Read more

Count the Number of Elements in a Python List

The len() function can be used to count the number of elements in a Python list: Copy len(my_list) 3 Examples of Counting the Number of Elements in a List Example 1: List that Contains Strings To start with a simple example, create a list that contains 5 names: Copy names_list = [“Jeff”, “Ben”, “Maria”, “Sophia”, … Read more

Create Horizontal Bar Chart using Matplotlib

Here is a simple template to create a horizontal bar chart using Matplotlib: Copy import matplotlib.pyplot as plty_axis = [“value_1”, “value_2”, “value_3”, …]x_axis = [“value_1”, “value_2”, “value_3”, …]plt.barh(y_axis, x_axis)plt.title(“title name”)plt.ylabel(“y axis name”)plt.xlabel(“x axis name”)plt.show() Steps to Create Horizontal Bar Chart using Matplotlib Step 1: Gather the data for the chart For example, let’s use the … Read more

How to Change the Order of Columns in Pandas DataFrame

In this short guide, you’ll see how to change the order of columns in Pandas DataFrame. Here are the steps: Steps to Change the Order of Columns in Pandas DataFrame Step 1: Prepare the Data for the DataFrame To begin, prepare the data for your DataFrame. For example: Name Age Profession Country Jack 22 Accountant … Read more

Find all Columns with NaN Values in Pandas DataFrame

Here are 4 ways to find all columns that contain NaN values in Pandas DataFrame: (1) Use isna() to find all columns with NaN values: Copy df.isna().any() (2) Use isnull() to find all columns with NaN values: Copy df.isnull().any() (3) Use isna() to select all columns with NaN values: Copy df[df.columns[df.isna().any()]] (4) Use isnull() to … Read more

Check the Version of the Python Interpreter

You can use the sys library in order to check the version of your Python Interpreter: Copy import sysprint(sys.version) Here is an example of a version of the Python Interpreter: Get the components of your version If you want to get the components of your version, you may use: Copy import sysprint(sys.version_info) For our example, … Read more

How to Control a Keyboard using Python

You can use the PyAutoGUI library to control a keyboard using Python. To start, here is the command to install the PyAutoGUI library: Copy pip install pyautogui In this tutorial, you’ll see 4 scenarios that describe how to: 4 Scenarios to Control a Keyboard using Python Scenario 1: Type characters using the typewrite() function You can … Read more

Transpose Pandas DataFrame

To transpose Pandas DataFrame: Copy df = df.transpose() Let’s see how to apply the above syntax by reviewing 3 cases of: Case 1: Transpose Pandas DataFrame with a Default Index To start with a simple example, let’s create a DataFrame with 3 columns: Copy import pandas as pd data = {‘A’: [11, 22, 33], ‘B’: … Read more