"
],
"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": [
"
"
],
"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": [
"
"
],
"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",
"
employee_id
\n",
"
salary
\n",
"
commission
\n",
"
\n",
" \n",
" \n",
"
\n",
"
0
\n",
"
1
\n",
"
1500.0
\n",
"
True
\n",
"
\n",
"
\n",
"
1
\n",
"
2
\n",
"
2000.0
\n",
"
True
\n",
"
\n",
"
\n",
"
2
\n",
"
3
\n",
"
2200.0
\n",
"
False
\n",
"
\n",
" \n",
"
\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": [
"