{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Data Frames - Basic Operations\n", "\n", "Here are some of the basic operations we typically perform on top of Pandas Data Frame.\n", "* Getting number of records and columns.\n", "* Getting data types of the columns.\n", "* Replacing `NaN` with some standard values.\n", "* Dropping a column from the Data Frame.\n", "* Getting or updating column names.\n", "* Sorting by index or values." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "tags": [ "remove-cell" ] }, "outputs": [ { "data": { "text/html": [ "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%HTML\n", "" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{note}\n", "Creating Pandas Data Frame using list of dicts.\n", "```" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "sals_ld = [\n", " {'id': 1, 'sal': 1500.0},\n", " {'id': 2, 'sal': 2000.0, 'comm': 10.0},\n", " {'id': 3, 'sal': 2200.0, 'active': False}\n", "]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{note}\n", "Column names will be inherited automatically using keys from the dict.\n", "```" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "sals_df = pd.DataFrame(sals_ld)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idsalcommactive
011500.0NaNNaN
122000.010.0NaN
232200.0NaNFalse
\n", "
" ], "text/plain": [ " id sal comm active\n", "0 1 1500.0 NaN NaN\n", "1 2 2000.0 10.0 NaN\n", "2 3 2200.0 NaN False" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sals_df" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 1\n", "1 2\n", "2 3\n", "Name: id, dtype: int64" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sals_df['id']" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idsal
011500.0
122000.0
232200.0
\n", "
" ], "text/plain": [ " id sal\n", "0 1 1500.0\n", "1 2 2000.0\n", "2 3 2200.0" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sals_df[['id', 'sal']]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, 4)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sals_df.shape" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sals_df.shape[0]" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "id 3\n", "sal 3\n", "comm 1\n", "active 1\n", "dtype: int64" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sals_df.count()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "id 3\n", "sal 3\n", "dtype: int64" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sals_df.count()[:2]" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sals_df.count()['id']" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idsalcommactive
011500.0NaNNaN
122000.010.0NaN
232200.0NaNFalse
\n", "
" ], "text/plain": [ " id sal comm active\n", "0 1 1500.0 NaN NaN\n", "1 2 2000.0 10.0 NaN\n", "2 3 2200.0 NaN False" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sals_df" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "id int64\n", "sal float64\n", "comm float64\n", "active object\n", "dtype: object" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sals_df.dtypes" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[0;31mSignature:\u001b[0m\n", "\u001b[0msals_df\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfillna\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mmethod\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0minplace\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mlimit\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mdowncast\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0mUnion\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0m_ForwardRef\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'DataFrame'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mNoneType\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mDocstring:\u001b[0m\n", "Fill NA/NaN values using the specified method.\n", "\n", "Parameters\n", "----------\n", "value : scalar, dict, Series, or DataFrame\n", " Value to use to fill holes (e.g. 0), alternately a\n", " dict/Series/DataFrame of values specifying which value to use for\n", " each index (for a Series) or column (for a DataFrame). Values not\n", " in the dict/Series/DataFrame will not be filled. This value cannot\n", " be a list.\n", "method : {'backfill', 'bfill', 'pad', 'ffill', None}, default None\n", " Method to use for filling holes in reindexed Series\n", " pad / ffill: propagate last valid observation forward to next valid\n", " backfill / bfill: use next valid observation to fill gap.\n", "axis : {0 or 'index', 1 or 'columns'}\n", " Axis along which to fill missing values.\n", "inplace : bool, default False\n", " If True, fill in-place. Note: this will modify any\n", " other views on this object (e.g., a no-copy slice for a column in a\n", " DataFrame).\n", "limit : int, default None\n", " If method is specified, this is the maximum number of consecutive\n", " NaN values to forward/backward fill. In other words, if there is\n", " a gap with more than this number of consecutive NaNs, it will only\n", " be partially filled. If method is not specified, this is the\n", " maximum number of entries along the entire axis where NaNs will be\n", " filled. Must be greater than 0 if not None.\n", "downcast : dict, default is None\n", " A dict of item->dtype of what to downcast if possible,\n", " or the string 'infer' which will try to downcast to an appropriate\n", " equal type (e.g. float64 to int64 if possible).\n", "\n", "Returns\n", "-------\n", "DataFrame or None\n", " Object with missing values filled or None if ``inplace=True``.\n", "\n", "See Also\n", "--------\n", "interpolate : Fill NaN values using interpolation.\n", "reindex : Conform object to new index.\n", "asfreq : Convert TimeSeries to specified frequency.\n", "\n", "Examples\n", "--------\n", ">>> df = pd.DataFrame([[np.nan, 2, np.nan, 0],\n", "... [3, 4, np.nan, 1],\n", "... [np.nan, np.nan, np.nan, 5],\n", "... [np.nan, 3, np.nan, 4]],\n", "... columns=list('ABCD'))\n", ">>> df\n", " A B C D\n", "0 NaN 2.0 NaN 0\n", "1 3.0 4.0 NaN 1\n", "2 NaN NaN NaN 5\n", "3 NaN 3.0 NaN 4\n", "\n", "Replace all NaN elements with 0s.\n", "\n", ">>> df.fillna(0)\n", " A B C D\n", "0 0.0 2.0 0.0 0\n", "1 3.0 4.0 0.0 1\n", "2 0.0 0.0 0.0 5\n", "3 0.0 3.0 0.0 4\n", "\n", "We can also propagate non-null values forward or backward.\n", "\n", ">>> df.fillna(method='ffill')\n", " A B C D\n", "0 NaN 2.0 NaN 0\n", "1 3.0 4.0 NaN 1\n", "2 3.0 4.0 NaN 5\n", "3 3.0 3.0 NaN 4\n", "\n", "Replace all NaN elements in column 'A', 'B', 'C', and 'D', with 0, 1,\n", "2, and 3 respectively.\n", "\n", ">>> values = {'A': 0, 'B': 1, 'C': 2, 'D': 3}\n", ">>> df.fillna(value=values)\n", " A B C D\n", "0 0.0 2.0 2.0 0\n", "1 3.0 4.0 2.0 1\n", "2 0.0 1.0 2.0 5\n", "3 0.0 3.0 2.0 4\n", "\n", "Only replace the first NaN element.\n", "\n", ">>> df.fillna(value=values, limit=1)\n", " A B C D\n", "0 0.0 2.0 2.0 0\n", "1 3.0 4.0 NaN 1\n", "2 NaN 1.0 NaN 5\n", "3 NaN 3.0 NaN 4\n", "\u001b[0;31mFile:\u001b[0m /opt/anaconda3/envs/beakerx/lib/python3.6/site-packages/pandas/core/frame.py\n", "\u001b[0;31mType:\u001b[0m method\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "sals_df.fillna?" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idsalcommactive
011500.00.00
122000.010.00
232200.00.0False
\n", "
" ], "text/plain": [ " id sal comm active\n", "0 1 1500.0 0.0 0\n", "1 2 2000.0 10.0 0\n", "2 3 2200.0 0.0 False" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sals_df.fillna(0.0)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idsalcommactive
011500.00.0NaN
122000.010.0NaN
232200.00.0False
\n", "
" ], "text/plain": [ " id sal comm active\n", "0 1 1500.0 0.0 NaN\n", "1 2 2000.0 10.0 NaN\n", "2 3 2200.0 0.0 False" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sals_df.fillna({'comm': 0.0})" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idsalcommactive
011500.00.0True
122000.010.0True
232200.00.0False
\n", "
" ], "text/plain": [ " id sal comm active\n", "0 1 1500.0 0.0 True\n", "1 2 2000.0 10.0 True\n", "2 3 2200.0 0.0 False" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sals_df.fillna({'comm': 0.0, 'active': True})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{note}\n", "Original Data Frame will be untouched, instead a new Data Frame will be created. Original Data Frame still contain `NaN`. We typically assign the output of most of the Data Frame functions to another variable or object.\n", "```" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idsalcommactive
011500.0NaNNaN
122000.010.0NaN
232200.0NaNFalse
\n", "
" ], "text/plain": [ " id sal comm active\n", "0 1 1500.0 NaN NaN\n", "1 2 2000.0 10.0 NaN\n", "2 3 2200.0 NaN False" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sals_df" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idsalcommactive
011500.00.0True
122000.010.0True
232200.00.0False
\n", "
" ], "text/plain": [ " id sal comm active\n", "0 1 1500.0 0.0 True\n", "1 2 2000.0 10.0 True\n", "2 3 2200.0 0.0 False" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sals_df = sals_df.fillna({'comm': 0.0, 'active': True})\n", "sals_df" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[0;31mSignature:\u001b[0m\n", "\u001b[0msals_df\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdrop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mlabels\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mindex\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mcolumns\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mlevel\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0minplace\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0merrors\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'raise'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mDocstring:\u001b[0m\n", "Drop specified labels from rows or columns.\n", "\n", "Remove rows or columns by specifying label names and corresponding\n", "axis, or by specifying directly index or column names. When using a\n", "multi-index, labels on different levels can be removed by specifying\n", "the level.\n", "\n", "Parameters\n", "----------\n", "labels : single label or list-like\n", " Index or column labels to drop.\n", "axis : {0 or 'index', 1 or 'columns'}, default 0\n", " Whether to drop labels from the index (0 or 'index') or\n", " columns (1 or 'columns').\n", "index : single label or list-like\n", " Alternative to specifying axis (``labels, axis=0``\n", " is equivalent to ``index=labels``).\n", "columns : single label or list-like\n", " Alternative to specifying axis (``labels, axis=1``\n", " is equivalent to ``columns=labels``).\n", "level : int or level name, optional\n", " For MultiIndex, level from which the labels will be removed.\n", "inplace : bool, default False\n", " If False, return a copy. Otherwise, do operation\n", " inplace and return None.\n", "errors : {'ignore', 'raise'}, default 'raise'\n", " If 'ignore', suppress error and only existing labels are\n", " dropped.\n", "\n", "Returns\n", "-------\n", "DataFrame\n", " DataFrame without the removed index or column labels.\n", "\n", "Raises\n", "------\n", "KeyError\n", " If any of the labels is not found in the selected axis.\n", "\n", "See Also\n", "--------\n", "DataFrame.loc : Label-location based indexer for selection by label.\n", "DataFrame.dropna : Return DataFrame with labels on given axis omitted\n", " where (all or any) data are missing.\n", "DataFrame.drop_duplicates : Return DataFrame with duplicate rows\n", " removed, optionally only considering certain columns.\n", "Series.drop : Return Series with specified index labels removed.\n", "\n", "Examples\n", "--------\n", ">>> df = pd.DataFrame(np.arange(12).reshape(3, 4),\n", "... columns=['A', 'B', 'C', 'D'])\n", ">>> df\n", " A B C D\n", "0 0 1 2 3\n", "1 4 5 6 7\n", "2 8 9 10 11\n", "\n", "Drop columns\n", "\n", ">>> df.drop(['B', 'C'], axis=1)\n", " A D\n", "0 0 3\n", "1 4 7\n", "2 8 11\n", "\n", ">>> df.drop(columns=['B', 'C'])\n", " A D\n", "0 0 3\n", "1 4 7\n", "2 8 11\n", "\n", "Drop a row by index\n", "\n", ">>> df.drop([0, 1])\n", " A B C D\n", "2 8 9 10 11\n", "\n", "Drop columns and/or rows of MultiIndex DataFrame\n", "\n", ">>> midx = pd.MultiIndex(levels=[['lama', 'cow', 'falcon'],\n", "... ['speed', 'weight', 'length']],\n", "... codes=[[0, 0, 0, 1, 1, 1, 2, 2, 2],\n", "... [0, 1, 2, 0, 1, 2, 0, 1, 2]])\n", ">>> df = pd.DataFrame(index=midx, columns=['big', 'small'],\n", "... data=[[45, 30], [200, 100], [1.5, 1], [30, 20],\n", "... [250, 150], [1.5, 0.8], [320, 250],\n", "... [1, 0.8], [0.3, 0.2]])\n", ">>> df\n", " big small\n", "lama speed 45.0 30.0\n", " weight 200.0 100.0\n", " length 1.5 1.0\n", "cow speed 30.0 20.0\n", " weight 250.0 150.0\n", " length 1.5 0.8\n", "falcon speed 320.0 250.0\n", " weight 1.0 0.8\n", " length 0.3 0.2\n", "\n", ">>> df.drop(index='cow', columns='small')\n", " big\n", "lama speed 45.0\n", " weight 200.0\n", " length 1.5\n", "falcon speed 320.0\n", " weight 1.0\n", " length 0.3\n", "\n", ">>> df.drop(index='length', level=1)\n", " big small\n", "lama speed 45.0 30.0\n", " weight 200.0 100.0\n", "cow speed 30.0 20.0\n", " weight 250.0 150.0\n", "falcon speed 320.0 250.0\n", " weight 1.0 0.8\n", "\u001b[0;31mFile:\u001b[0m /opt/anaconda3/envs/beakerx/lib/python3.6/site-packages/pandas/core/frame.py\n", "\u001b[0;31mType:\u001b[0m method\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "sals_df.drop?" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idsalactive
011500.0True
122000.0True
232200.0False
\n", "
" ], "text/plain": [ " id sal active\n", "0 1 1500.0 True\n", "1 2 2000.0 True\n", "2 3 2200.0 False" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sals_df.drop(columns='comm')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{note}\n", "We can also drop multiple columns by passing column names as list.\n", "```" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idsal
011500.0
122000.0
232200.0
\n", "
" ], "text/plain": [ " id sal\n", "0 1 1500.0\n", "1 2 2000.0\n", "2 3 2200.0" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sals_df.drop(columns=['comm', 'active'])" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idsal
011500.0
122000.0
232200.0
\n", "
" ], "text/plain": [ " id sal\n", "0 1 1500.0\n", "1 2 2000.0\n", "2 3 2200.0" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sals_df.drop(['comm', 'active'], axis=1)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "sals_df = sals_df.drop(columns='comm')" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Index(['id', 'sal', 'active'], dtype='object')" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sals_df.columns" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "sals_df.columns = ['employee_id', 'salary', 'commission']" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
employee_idsalarycommission
011500.0True
122000.0True
232200.0False
\n", "
" ], "text/plain": [ " employee_id salary commission\n", "0 1 1500.0 True\n", "1 2 2000.0 True\n", "2 3 2200.0 False" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sals_df" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[0;31mSignature:\u001b[0m\n", "\u001b[0msals_df\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msort_index\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mlevel\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mascending\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0mbool\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0minplace\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0mbool\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mkind\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0mstr\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'quicksort'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mna_position\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0mstr\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'last'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0msort_remaining\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0mbool\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mignore_index\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0mbool\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0mUnion\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mCallable\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0m_ForwardRef\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Index'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mUnion\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0m_ForwardRef\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Index'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m~\u001b[0m\u001b[0mAnyArrayLike\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mNoneType\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mDocstring:\u001b[0m\n", "Sort object by labels (along an axis).\n", "\n", "Returns a new DataFrame sorted by label if `inplace` argument is\n", "``False``, otherwise updates the original DataFrame and returns None.\n", "\n", "Parameters\n", "----------\n", "axis : {0 or 'index', 1 or 'columns'}, default 0\n", " The axis along which to sort. The value 0 identifies the rows,\n", " and 1 identifies the columns.\n", "level : int or level name or list of ints or list of level names\n", " If not None, sort on values in specified index level(s).\n", "ascending : bool or list of bools, default True\n", " Sort ascending vs. descending. When the index is a MultiIndex the\n", " sort direction can be controlled for each level individually.\n", "inplace : bool, default False\n", " If True, perform operation in-place.\n", "kind : {'quicksort', 'mergesort', 'heapsort'}, default 'quicksort'\n", " Choice of sorting algorithm. See also ndarray.np.sort for more\n", " information. `mergesort` is the only stable algorithm. For\n", " DataFrames, this option is only applied when sorting on a single\n", " column or label.\n", "na_position : {'first', 'last'}, default 'last'\n", " Puts NaNs at the beginning if `first`; `last` puts NaNs at the end.\n", " Not implemented for MultiIndex.\n", "sort_remaining : bool, default True\n", " If True and sorting by level and index is multilevel, sort by other\n", " levels too (in order) after sorting by specified level.\n", "ignore_index : bool, default False\n", " If True, the resulting axis will be labeled 0, 1, …, n - 1.\n", "\n", " .. versionadded:: 1.0.0\n", "\n", "key : callable, optional\n", " If not None, apply the key function to the index values\n", " before sorting. This is similar to the `key` argument in the\n", " builtin :meth:`sorted` function, with the notable difference that\n", " this `key` function should be *vectorized*. It should expect an\n", " ``Index`` and return an ``Index`` of the same shape. For MultiIndex\n", " inputs, the key is applied *per level*.\n", "\n", " .. versionadded:: 1.1.0\n", "\n", "Returns\n", "-------\n", "DataFrame\n", " The original DataFrame sorted by the labels.\n", "\n", "See Also\n", "--------\n", "Series.sort_index : Sort Series by the index.\n", "DataFrame.sort_values : Sort DataFrame by the value.\n", "Series.sort_values : Sort Series by the value.\n", "\n", "Examples\n", "--------\n", ">>> df = pd.DataFrame([1, 2, 3, 4, 5], index=[100, 29, 234, 1, 150],\n", "... columns=['A'])\n", ">>> df.sort_index()\n", " A\n", "1 4\n", "29 2\n", "100 1\n", "150 5\n", "234 3\n", "\n", "By default, it sorts in ascending order, to sort in descending order,\n", "use ``ascending=False``\n", "\n", ">>> df.sort_index(ascending=False)\n", " A\n", "234 3\n", "150 5\n", "100 1\n", "29 2\n", "1 4\n", "\n", "A key function can be specified which is applied to the index before\n", "sorting. For a ``MultiIndex`` this is applied to each level separately.\n", "\n", ">>> df = pd.DataFrame({\"a\": [1, 2, 3, 4]}, index=['A', 'b', 'C', 'd'])\n", ">>> df.sort_index(key=lambda x: x.str.lower())\n", " a\n", "A 1\n", "b 2\n", "C 3\n", "d 4\n", "\u001b[0;31mFile:\u001b[0m /opt/anaconda3/envs/beakerx/lib/python3.6/site-packages/pandas/core/frame.py\n", "\u001b[0;31mType:\u001b[0m method\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "sals_df.sort_index?" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
employee_idsalarycommission
011500.0True
122000.0True
232200.0False
\n", "
" ], "text/plain": [ " employee_id salary commission\n", "0 1 1500.0 True\n", "1 2 2000.0 True\n", "2 3 2200.0 False" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sals_df.sort_index(ascending=False)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[0;31mSignature:\u001b[0m\n", "\u001b[0msals_df\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msort_values\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mby\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mascending\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0minplace\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mkind\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'quicksort'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mna_position\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'last'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mignore_index\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0mUnion\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mCallable\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0m_ForwardRef\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Series'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mUnion\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0m_ForwardRef\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Series'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m~\u001b[0m\u001b[0mAnyArrayLike\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mNoneType\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mDocstring:\u001b[0m\n", "Sort by the values along either axis.\n", "\n", "Parameters\n", "----------\n", " by : str or list of str\n", " Name or list of names to sort by.\n", "\n", " - if `axis` is 0 or `'index'` then `by` may contain index\n", " levels and/or column labels.\n", " - if `axis` is 1 or `'columns'` then `by` may contain column\n", " levels and/or index labels.\n", "\n", " .. versionchanged:: 0.23.0\n", "\n", " Allow specifying index or column level names.\n", "axis : {0 or 'index', 1 or 'columns'}, default 0\n", " Axis to be sorted.\n", "ascending : bool or list of bool, default True\n", " Sort ascending vs. descending. Specify list for multiple sort\n", " orders. If this is a list of bools, must match the length of\n", " the by.\n", "inplace : bool, default False\n", " If True, perform operation in-place.\n", "kind : {'quicksort', 'mergesort', 'heapsort'}, default 'quicksort'\n", " Choice of sorting algorithm. See also ndarray.np.sort for more\n", " information. `mergesort` is the only stable algorithm. For\n", " DataFrames, this option is only applied when sorting on a single\n", " column or label.\n", "na_position : {'first', 'last'}, default 'last'\n", " Puts NaNs at the beginning if `first`; `last` puts NaNs at the\n", " end.\n", "ignore_index : bool, default False\n", " If True, the resulting axis will be labeled 0, 1, …, n - 1.\n", "\n", " .. versionadded:: 1.0.0\n", "\n", "key : callable, optional\n", " Apply the key function to the values\n", " before sorting. This is similar to the `key` argument in the\n", " builtin :meth:`sorted` function, with the notable difference that\n", " this `key` function should be *vectorized*. It should expect a\n", " ``Series`` and return a Series with the same shape as the input.\n", " It will be applied to each column in `by` independently.\n", "\n", " .. versionadded:: 1.1.0\n", "\n", "Returns\n", "-------\n", "DataFrame or None\n", " DataFrame with sorted values if inplace=False, None otherwise.\n", "\n", "See Also\n", "--------\n", "DataFrame.sort_index : Sort a DataFrame by the index.\n", "Series.sort_values : Similar method for a Series.\n", "\n", "Examples\n", "--------\n", ">>> df = pd.DataFrame({\n", "... 'col1': ['A', 'A', 'B', np.nan, 'D', 'C'],\n", "... 'col2': [2, 1, 9, 8, 7, 4],\n", "... 'col3': [0, 1, 9, 4, 2, 3],\n", "... 'col4': ['a', 'B', 'c', 'D', 'e', 'F']\n", "... })\n", ">>> df\n", " col1 col2 col3 col4\n", "0 A 2 0 a\n", "1 A 1 1 B\n", "2 B 9 9 c\n", "3 NaN 8 4 D\n", "4 D 7 2 e\n", "5 C 4 3 F\n", "\n", "Sort by col1\n", "\n", ">>> df.sort_values(by=['col1'])\n", " col1 col2 col3 col4\n", "0 A 2 0 a\n", "1 A 1 1 B\n", "2 B 9 9 c\n", "5 C 4 3 F\n", "4 D 7 2 e\n", "3 NaN 8 4 D\n", "\n", "Sort by multiple columns\n", "\n", ">>> df.sort_values(by=['col1', 'col2'])\n", " col1 col2 col3 col4\n", "1 A 1 1 B\n", "0 A 2 0 a\n", "2 B 9 9 c\n", "5 C 4 3 F\n", "4 D 7 2 e\n", "3 NaN 8 4 D\n", "\n", "Sort Descending\n", "\n", ">>> df.sort_values(by='col1', ascending=False)\n", " col1 col2 col3 col4\n", "4 D 7 2 e\n", "5 C 4 3 F\n", "2 B 9 9 c\n", "0 A 2 0 a\n", "1 A 1 1 B\n", "3 NaN 8 4 D\n", "\n", "Putting NAs first\n", "\n", ">>> df.sort_values(by='col1', ascending=False, na_position='first')\n", " col1 col2 col3 col4\n", "3 NaN 8 4 D\n", "4 D 7 2 e\n", "5 C 4 3 F\n", "2 B 9 9 c\n", "0 A 2 0 a\n", "1 A 1 1 B\n", "\n", "Sorting with a key function\n", "\n", ">>> df.sort_values(by='col4', key=lambda col: col.str.lower())\n", " col1 col2 col3 col4\n", "0 A 2 0 a\n", "1 A 1 1 B\n", "2 B 9 9 c\n", "3 NaN 8 4 D\n", "4 D 7 2 e\n", "5 C 4 3 F\n", "\u001b[0;31mFile:\u001b[0m /opt/anaconda3/envs/beakerx/lib/python3.6/site-packages/pandas/core/frame.py\n", "\u001b[0;31mType:\u001b[0m method\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "sals_df.sort_values?" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
employee_idsalarycommission
232200.0False
122000.0True
011500.0True
\n", "
" ], "text/plain": [ " employee_id salary commission\n", "2 3 2200.0 False\n", "1 2 2000.0 True\n", "0 1 1500.0 True" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sals_df.sort_values(by='employee_id', ascending=False)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
employee_idsalarycommission
011500.0True
122000.0True
232200.0False
\n", "
" ], "text/plain": [ " employee_id salary commission\n", "0 1 1500.0 True\n", "1 2 2000.0 True\n", "2 3 2200.0 False" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sals_df.sort_values(by='salary')" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
employee_idsalarycommission
232200.0False
122000.0True
011500.0True
\n", "
" ], "text/plain": [ " employee_id salary commission\n", "2 3 2200.0 False\n", "1 2 2000.0 True\n", "0 1 1500.0 True" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sals_df.sort_values(by='salary', ascending=False)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "sals_ld = [\n", " {'id': 1, 'sal': 1500.0},\n", " {'id': 2, 'sal': 2000.0, 'comm': 10.0},\n", " {'id': 3, 'sal': 2200.0, 'active': False},\n", " {'id': 4, 'sal': 2000.0}\n", "]" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [], "source": [ "sals_df = pd.DataFrame(sals_ld)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idsalcommactive
011500.0NaNNaN
122000.010.0NaN
342000.0NaNNaN
232200.0NaNFalse
\n", "
" ], "text/plain": [ " id sal comm active\n", "0 1 1500.0 NaN NaN\n", "1 2 2000.0 10.0 NaN\n", "3 4 2000.0 NaN NaN\n", "2 3 2200.0 NaN False" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sals_df.sort_values(by=['sal', 'id'])" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idsalcommactive
232200.0NaNFalse
122000.010.0NaN
342000.0NaNNaN
011500.0NaNNaN
\n", "
" ], "text/plain": [ " id sal comm active\n", "2 3 2200.0 NaN False\n", "1 2 2000.0 10.0 NaN\n", "3 4 2000.0 NaN NaN\n", "0 1 1500.0 NaN NaN" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sals_df.sort_values(by=['sal', 'id'], ascending=[False, True])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.6.12" } }, "nbformat": 4, "nbformat_minor": 4 }