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 swaplevel()
The swaplevel()
method trades the contents of level i
with the contents of level j
in a MultiIndex
DataFrame/Series.
The syntax for this method is as follows:
DataFrame.swaplevel(i=- 2, j=- 1, axis=0)
Parameter | Description |
---|---|
i , j | These parameters can be an integer/string. They are the indexes to be swapped. |
axis | If zero (0) or index is selected, apply to each column. Default is 0 (column). If zero (1) or columns, apply to each row. |
For this example, there are five (5) students. Each student has some associated data with it. Grades generate by using np.random.randint()
from the NumPy library.
index = [(1001, 'Micah Smith', 14), (1001, 'Philip Jones', 15), (1002, 'Ben Grimes', 16), (1002, 'Alicia Heath', 17), (1002, 'Arch Nelson', 18)] m_index = pd.MultiIndex.from_tuples(index) grades_lst = np.random.randint(45,100,size=5) df = pd.DataFrame({"Grades": grades_lst}, index=m_index) print(df) result = df.swaplevel(0,1) print(result)
- Line [1] creates a list of tuples. Each tuple contains three (3) values. The output saves to the index.
- Line [2] creates a
MultiIndex
from the List of Tuples created on line [1] and saves it tom_index
. - Line [3] generates five (5) random grades between the specified range and saves them to
grades_lst
. - Line [4] creates a DataFrame from the variables created on lines [1-3] and saves to
df
. - Line [5] outputs the DataFrame to the terminal.
- Line [6] swaps out the levels as specified. The output saves to
result
. - Line [7] outputs the result to the terminal.
Output
df
Grades | |||
1001 | Micah Smith | 14 | 55 |
Philip Jones | 15 | 74 | |
1002 | Ben Grimes | 16 | 93 |
Alicia Heath | 17 | 93 | |
Arch Nelson | 18 | 63 |
result
Grades | |||
Micah Smith | 1001 | 14 | 55 |
Philip Jones | 1001 | 15 | 74 |
Ben Grimes | 1002 | 16 | 93 |
Alicia Heath | 1002 | 17 | 93 |
Arch Nelson | 1002 | 18 | 63 |
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.