Removing columns from a pandas DataFrame

In this section, we'll look at how to remove columns or rows from a dataset in pandas. We will come to understand the drop() method and the functionality of its parameters in detail.

To start with, we first import the pandas module into our Jupyter notebook:

import pandas as pd

After this, we read our CSV dataset using the following code:

data = pd.read_csv('data-titanic.csv', index_col=3)
data.head()

The dataset should look something like the following:

To remove a single column from our dataset, the pandas drop() method is used. The drop() method consists of two parameters. The first parameter is the name of the column that needs to be eliminated; the second parameter is the axis. This parameter tells the drop method whether it should drop a row or column, and sets inplace to True, which tells the method to drop it from the original DataFrame itself.

In this example, let's consider removing the Ticket column. The code for this is as follows:

data.drop('Ticket', axis=1, inplace=True)

Once this is executed, our dataset should look something like the following:

data.head()

If we observe closely, it is clear that the Ticket column has been removed, or dropped, from our dataset.

To remove multiple columns, we pass the ones that need to be dropped as a list to the drop() method. All the other parameters of the drop() method will stay the same. 

Let's look at an example of how to eliminate rows using the drop() method.

In this example, we shall be dropping multiple rows. Thus, instead of passing the column names, we will pass the row index labels in the form of a list. The following code will be used to do this:

data.drop(['Parch', 'Fare'], axis=1, inplace=True)
data.head()

As a result, the two rows, which correspond to the names of the passengers, passed to the drop() method, will be taken off the dataset.

We will now proceed to take a closer look at how to work with date and time data.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.23.59.248