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 reset_index()
The reset_index()
method resets the DataFrames index and reverts the DataFrame to the default (original) index.
The syntax for this method is as follows:
DataFrame.reset_index(level=None, drop=False, inplace=False, col_level=0, col_fill='')
Parameter | Description |
---|---|
level | This parameter can be an integer, string, tuple, or list-like. It removes said levels from the index. By default, this parameter removes all levels. |
drop | Do not insert an index into a DataFrame column. This option will reset the index to the original integer index. |
col_level | If multi-level, this parameter determines the insertion level. By default, use the first level. |
For this example, we have three (3) Classical Composers with some details about their life. They will be assigned levels based on the difficulty of their compositions.
Code β reset_index()
data = {'Composer': ['Chopin', 'Listz', 'Haydn'], 'Born': [1810, 1811, 1732], 'Country': ['France', 'Austria', 'Austria']} index = {'Level-1', 'Level-2', 'Level-3'} df = pd.DataFrame(data, index) print(df) df.reset_index(inplace=True, drop=True) print(df)
- Line [1] creates a dictionary of lists and saves it to
data
. - Line [2] sets index labels for the Composers and saves them to the variable
index
. - Line [3] creates a DataFrame and assigns it to
df
. - Line [4] outputs the result to the terminal.
- Line [5] resets the DataFrame index (
reset_index()
) back to the original integer index. - Line [6] outputs the result to the terminal.
Output
df
Composer | Born | Country | |
Level-1 | Chopin | 1810 | France |
Level-3 | Listz | 1811 | Austria |
Level-2 | Haydn | 1732 | Austria |
result
Composer | Born | Country | |
0 | Chopin | 1810 | France |
1 | Listz | 1811 | Austria |
2 | Haydn | 1732 | Austria |
Another way to accomplish the above task is to use the concat()
method.
Code β concat()
data = {'Composer': ['Chopin', 'Listz', 'Haydn'], 'Born': [1810, 1811, 1732], 'Country': ['France', 'Austria', 'Austria']} index = {'Level-1', 'Level-2', 'Level-3'} df = pd.DataFrame(data, index) print(df) df1 = pd.concat([df], ignore_index=True) print(df)
- Line [1] creates a dictionary of lists and saves it to
data
. - Line [2] sets index labels for the Composers and saves them to the variable
index
. - Line [3] creates a DataFrame and assigns it to
df
. - Line [4] outputs the result to the terminal.
- Line [5] resets the DataFrame index (
concat()
) back to the original integer index. - Line [6] outputs the result to the terminal.
Output
df
Composer | Born | Country | |
Level-1 | Chopin | 1810 | France |
Level-3 | Listz | 1811 | Austria |
Level-2 | Haydn | 1732 | Austria |
result
Composer | Born | Country | |
0 | Chopin | 1810 | France |
1 | Listz | 1811 | Austria |
2 | Haydn | 1732 | Austria |
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.