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 abs()
The abs()
method converts each element in a DataFrame containing a negative value to a positive (absolute) value. This method has no parameters. Another option aside from the abs()
method is to use numpy.absolute()
.
The syntax for this method is as follows:
DataFrame.abs()
For this example, the Sales Manager of Rivers Clothing noticed that some of their inventory contained negative pricing. To resolve this issue, the Sales Manager ran the following code.
Code β Example 1
df_inv = pd.DataFrame({'Tops': [36, 23, 19], 'Tanks': [44, 43, -20], 'Pants': [61, -33, 67], 'Sweats': [88, 38, 13]}) index_ = ['Small', 'Medium', 'Large'] df_inv.index = index_ result = df_inv.abs() print(result)
- Line [1] creates a DataFrame from a dictionary of lists and saves it to
df_inv
. - Line [2-3] creates and sets the index for the DataFrame (Small/Medium/Large).
- Line [4] uses the
abs()
method to convert negative values to positive (absolute) values. The output saves to theresult
variable. - Line [5] outputs the result to the terminal.
Output
Tops | Tanks | Pants | Sweats | |
Small | 36 | 44 | 61 | 88 |
Medium | 23 | 43 | 33 | 38 |
Large | 19 | 20 | 67 | 13 |
This example is similar to the above. However, it calls numpy.absolute()
to change negative values to positive (absolute) values. The output remains the same.
Code β Example 2
df_inv = pd.DataFrame({'Tops': [36, 23, 19], 'Tanks': [44, 43, -20], 'Pants': [61, -33, 67], 'Sweats': [88, 38, 13]}) index_ = ['Small', 'Medium', 'Large'] df_inv.index = index_ result = np.absolute(df_inv) print(result)
- Line [1] creates a DataFrame from a Dictionary of Lists and saves it to
df_inv
. - Line [2-3] creates and sets the index for the DataFrame (Small/Medium/Large).
- Line [4] uses
np.absolute()
to convert any negative values to positive (absolute) values. The output saves to theresult
variable. - Line [5] outputs the result to the terminal. The output is identical to the example above.
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.