{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Examples**"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"countries_population = {\"Italy\": 60550000, \"France\": 65130728,\n",
" \"Russia\": 435000, \"Iceland\": 435000,\n",
" \"Palau\": 435000, \"Brazil\": 21104900,\n",
" \"Nauru\": 11600, \"Tuvalu\": 11600,\n",
" \"Bermuda\": 11600, \"Tokelau\": 1440}"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Italy 60550000\n",
"France 65130728\n",
"Russia 435000\n",
"Iceland 435000\n",
"Palau 435000\n",
"Brazil 21104900\n",
"Nauru 11600\n",
"Tuvalu 11600\n",
"Bermuda 11600\n",
"Tokelau 1440\n",
"dtype: int64"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s = pd.Series(countries_population)\n",
"s"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The n largest elements where n=5 by default."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"France 65130728\n",
"Italy 60550000\n",
"Brazil 21104900\n",
"Russia 435000\n",
"Iceland 435000\n",
"dtype: int64"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s.nlargest()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The n largest elements where n=4. Default keep value is ‘first’ so Russia will be kept."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"France 65130728\n",
"Italy 60550000\n",
"Brazil 21104900\n",
"Russia 435000\n",
"dtype: int64"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s.nlargest(4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The n largest elements where n=4 and keeping the last duplicates. Palau will be kept since
\n",
"it is the last with value 435000 based on the index order."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"France 65130728\n",
"Italy 60550000\n",
"Brazil 21104900\n",
"Palau 435000\n",
"dtype: int64"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s.nlargest(4, keep='last')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The n largest elements where n=5 with all duplicates kept. Note that the returned Series has five elements
\n",
"due to the three duplicates."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"France 65130728\n",
"Italy 60550000\n",
"Brazil 21104900\n",
"Russia 435000\n",
"Iceland 435000\n",
"Palau 435000\n",
"dtype: int64"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s.nlargest(5, keep='all')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}