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 set_axis()
The set_axis()
method assigns index(es) to the selected axis.
The syntax for this method is as follows:
DataFrame.set_axis(labels, axis=0, inplace=False)
Parameter | Description |
---|---|
labels | This parameter is a list or a list-like object containing index labels. |
axis | If zero (0) or index is selected, apply to each column. Default 0. If one (1) apply to each row. |
inplace | If False , a copy of the original DataFrame/Series is updated. This parameter is None , by default. |
For these examples, the index saves to the selected axis.
In this example, we set the axis to the row index.
Code β Example 1
df = pd.DataFrame({'Micah': [123, 120, 144], 'Paula': [129, 125, 90], 'Chloe': [101, 95, 124]}) print(df) result = df.set_axis(['Day-1', 'Day-2', 'Day-3'], axis='index') print(result)
- Line [1] creates a dictionary of lists and saves it to
df
. - Line [2] outputs the DataFrame (
df
) to the terminal. - Line [3] sets the new axis for the DataFrame and saves it to the
result
variable. - Line [4] outputs the result to the terminal.
Output
df
Micah | Paula | Chloe | |
0 | 123 | 129 | 101 |
1 | 120 | 125 | 95 |
2 | 144 | 90 | 124 |
result
Micah | Paula | Chloe | |
Day-1 | 123 | 129 | 101 |
Day-2 | 120 | 125 | 95 |
Day-3 | 144 | 90 | 124 |
In this example, we set the axis to the column index.
Code β Example 2
df = pd.DataFrame({'Micah': [123, 120, 144], 'Paula': [129, 125, 90], 'Chloe': [101, 95, 124]}) print(df) result = df.set_axis(['Micah M', 'Paula D', 'Chloe J'], axis='columns') print(result)
- Line [1] creates a dictionary of lists and saves it to
df
. - Line [2] outputs the DataFrame (
df
) to the terminal. - Line [3] sets the new axis for the DataFrame and saves it to the
result
variable. - Line [4] outputs the result to the terminal.
Output
df
Micah | Paula | Chloe | |
0 | 123 | 129 | 101 |
1 | 120 | 125 | 95 |
2 | 144 | 90 | 124 |
result
Micah M | Paula D | Chloe J | |
0 | 123 | 129 | 101 |
1 | 120 | 125 | 95 |
2 | 144 | 90 | 124 |
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.