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 rank()
The rank()
method returns a DataFrame/Series with the values ranked in order. The return value is the same as the caller.
The syntax for this method is as follows:
DataFrame.rank(axis=0, method='average', numeric_only=None, na_option='keep', ascending=True, pct=False)
Parameter | Description |
---|---|
axis | If zero (0) or index is selected, apply to each column. Default 0. If one (1) apply to each row. |
method | Determines how to rank identical values, such as: – The average rank of the group. – The lowest (min) rank value of the group. – The highest (max) rank value of the group. – Each assigns in the same order they appear in the array. – Density increases by one (1) between the groups. |
numeric_only | Only include columns that contain integers, floats, or boolean values. |
na_option | Determines how NaN values rank, such as: – Keep assigns a NaN to the rank values. – Top: The lowest rank to any NaN values found. – Bottom: The highest to any NaN values found. |
ascending | Determines if the elements/values rank in ascending or descending order. |
pct | If set to True , the results will return in percentile form. By default, this value is False . |
For this example, a CSV file is read in and is ranked on Population and sorted. Click here to download and move this file to the current working directory.
df = pd.read_csv("countries.csv") df["Rank"] = df["Population"].rank() df.sort_values("Population", inplace=True) print(df)
- Line [1] reads in the
countries.csv
file and saves it todf
. - Line [2] appends a column to the end of the DataFrame (
df
). - Line [3] sorts the CSV file in ascending order.
- Line [4] outputs the result to the terminal.
Output
Country | Capital | Population | Area | Rank | |
4 | Poland | Warsaw | 38383000 | 312685 | 1.0 |
2 | Spain | Madrid | 47431256 | 498511 | 2.0 |
3 | Italy | Rome | 60317116 | 301338 | 3.0 |
1 | France | Paris | 67081000 | 551695 | 4.0 |
0 | Germany | Berlin | 83783942 | 357021 | 5.0 |
5 | Russia | Moscow | 146748590 | 17098246 | 6.0 |
6 | USA | Washington | 328239523 | 9833520 | 7.0 |
8 | India | Dheli | 1352642280 | 3287263 | 8.0 |
7 | China | Beijing | 1400050000 | 9596961 | 9.0 |
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.