How to fix the Nameerror name pd is not defined error?

Solve the name error pd not defined Make sure to import the pandas library into your Python script or Jupyter notebook and assign it the alias pd before invoking any of the library methods: Reproducing the name error Suppose that you run the following snippet in your Jupyter Notebook, Pycharm , Spyder, VS Code or … Read more

How to drop the last column of a Pandas DataFrame?

Remove last column from pandas DataFrame In a nutshell, you are able to drop the last column of your DataFrame using one of the following techniques: By column index: or alternatively by column name: Create example data First, we will import pandas and create some data that you can use to follow along: Let’s look … Read more

How to filter and select columns by name in Pandas?

In this tutorial we’ll learn how to select one or multiple specific columns of a pandas DataFrame by name or index. Filter pandas column by name In a nutshell, use the pandas filter method to select one or multiple specific dataframe columns by their name: Initialize a DataFrame We will first go ahead and create … Read more

How to fill a column with single values in Pandas?

In today’s tutorial we’ll learn how to write specific values into a new or existing pandas DataFrame column. We’ll look into several use cases. Example data We will start by defining a simple dataset: Here’s our data: dates sales 0 2024-03-01 284 1 2024-03-04 472 2 2024-03-05 361 3 2024-03-06 269 4 2024-03-07 386 5 … Read more

How to change nan values to zero in pandas DataFrames?

Use the following code to replace empty values with zeros in a complete pandas DataFrame, one column or multiple ones: Example DataFrame We’ll import Data Analysis libraries pandas and numpy. Numpy will be used to generate a empty values in the Python lists that we will use in order to build your DataFrame. We will … Read more

How to convert a pandas column to numeric types?

This tutorial will teach you how to cast a pandas column of objects to numeric types such as integer or floats. This is a common task you will typically execute during the data cleaning phase of your data acquisition workflow; typically when harmonizing the data types in a column that contains figures, but also non-numeric … Read more

How to drop a pandas column if it exists?

The key challenge when automating your data wrangling process is to gracefully handle possible exceptions. As an example, as part of the automation, might need to drop one or more columns from the DataFrame (provided they exists). In this tutorial we’ll learn how to check if columns exist in our DataFrame, and if so, remove … Read more