Preparation
Before any data manipulation can occur, two (2) new libraries will require installation.
- The Pandas library enables access to/from a DataFrame.
- The NumPy library supports multi-dimensional arrays and matrices in addition to a collection of mathematical functions.
To install these libraries, navigate to an IDE terminal. At the command prompt ($
), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($
). Your terminal prompt may be different.
$ pip install pandas
Hit the <Enter>
key on the keyboard to start the installation process.
$ pip install numpy
Hit the <Enter>
key on the keyboard to start the installation process.
If the installations were successful, a message displays in the terminal indicating the same.
Feel free to view the PyCharm installation guide for the required libraries.
Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.
import pandas as pd import numpy as np
DataFrame drop()
The drop()
method deletes rows/columns from a DataFrame by entering a label name(s) and an axis or entering the index/column name(s). To fully understand this method, feel free to watch this short tutorial:
The syntax for this method is as follows:
DataFrame.drop(labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise')
Parameter | Description |
---|---|
labels | A single label or list of label(s) to delete from the DataFrame. |
| If zero (0) or index is selected, apply to each column. Default 0. If one (1) apply to each row. |
index | Rather than entering an index axis: axis=0 , you could enter index=labels . |
columns | Rather than entering a column axis: axis=1 , you could enter columns=labels . |
level | If multi-index, enter the appropriate level to delete. |
inplace | If False , create a copy of the DataFrame/Series. If zero (0) or index is selected, apply to each column. Default is None . If True , the original DataFrame/Series updates. |
errors | If set to ignore , the errors suppress. |
For this example, Rivers Clothing has decided to drop Sweats from its clothing line. They need this clothing line removed. To perform this task, run the following code:
Code Example 1 (Simple Drop)
df = pd.DataFrame({'Tops': [10, 12, 13], 'Tanks': [11, 13, 14], 'Pants': [21, 56, 94], 'Sweats': [27, 21, 35]}, index=['Small', 'Medium', 'Large']) result = df.drop('Sweats', axis=1) print(result)
- Line [1] creates a DataFrame and saves it to
df
. - Line [2] drops the Sweats data from the DataFrame and saves it to the
result
variable. - Line [3] outputs the result to the terminal.
Output
Tops | Tanks | Pants | |
Small | 10 | 11 | 21 |
Medium | 12 | 13 | 56 |
Large | 13 | 14 | 94 |
For this simple example, we drop a level from a DataFrame with a MultiIndex
.
Code Example 2 (Multi-Level Drop)
cols = pd.MultiIndex.from_tuples([("A", "a"), ("C", "c"), ("E", "e")]) df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=cols) print(df)
Setting up the MultiIndex & DataFrame
- Line [1] creates a
MultiIndex
from tuples and saves it tocols
. - Line [2] creates a DataFrame with random data and sets the columns to
cols
. - Line [3] outputs the result to the terminal.
df.columns = df.columns.droplevel(0) print(df)
- Line [4] uses
droplevel()
to drop the first column. - Line [5] outputs the result to the terminal.
π‘Note: Replacing Line [4] with: df.columns = [col[0] for col in df.columns]
accomplishes the same outcome using list comprehension.
Output
Original
A | C | E | |
a | c | e | |
0 | 1 | 2 | 3 |
1 | 4 | 5 | 6 |
After Drop
a | c | e | |
0 | 1 | 2 | 3 |
1 | 4 | 5 | 6 |
More Pandas DataFrame Methods
Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:
Also, check out the full cheat sheet overview of all Pandas DataFrame methods.