User Tools

Site Tools


playground:transclude_jupyter_notebook_from_github

Dokuwiki is unable to render an ipython notebook correctly. Compare the output below with https://github.com/KamarajuKusumanchi/notebooks/blob/master/pandas/set%20index%20from%200%20to%20N%20using%20reset_index.ipynb .

pandas/set%20index%20from%200%20to%20N%20using%20reset_index.ipynb
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0e50f142",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     A      B         C         D         E     F\n",
      "0  foo    one  0.496714 -0.469474 -1.012831  None\n",
      "1  bar    one -0.138264  0.542560  0.314247  True\n",
      "2  foo    two  0.647689 -0.463418 -0.908024  None\n",
      "3  bar  three  1.523030 -0.465730 -1.412304  None\n",
      "4  foo    two -0.234153  0.241962  1.465649  None\n",
      "5  bar    two -0.234137 -1.913280 -0.225776  True\n",
      "6  foo    one  1.579213 -1.724918  0.067528  None\n",
      "7  foo  three  0.767435 -0.562288 -1.424748  True\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "np.random.seed(42)\n",
    "df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',\n",
    "                          'foo', 'bar', 'foo', 'foo'],\n",
    "                   'B' : ['one', 'one', 'two', 'three',\n",
    "                          'two', 'two', 'one', 'three'],\n",
    "                   'C' : np.random.randn(8),\n",
    "                   'D' : np.random.randn(8),\n",
    "                   'E' : np.random.randn(8),\n",
    "                   'F' : [None, True, None, None, None, True, None, True]\n",
    "                  })\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ef5a4a9a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     A      B         C         D         E     F\n",
      "5  bar    two -0.234137 -1.913280 -0.225776  True\n",
      "6  foo    one  1.579213 -1.724918  0.067528  None\n",
      "7  foo  three  0.767435 -0.562288 -1.424748  True\n",
      "0  foo    one  0.496714 -0.469474 -1.012831  None\n",
      "3  bar  three  1.523030 -0.465730 -1.412304  None\n",
      "2  foo    two  0.647689 -0.463418 -0.908024  None\n",
      "4  foo    two -0.234153  0.241962  1.465649  None\n",
      "1  bar    one -0.138264  0.542560  0.314247  True\n"
     ]
    }
   ],
   "source": [
    "df.sort_values('D', inplace=True)\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fac53f86",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     A      B         C         D         E     F\n",
      "0  bar    two -0.234137 -1.913280 -0.225776  True\n",
      "1  foo    one  1.579213 -1.724918  0.067528  None\n",
      "2  foo  three  0.767435 -0.562288 -1.424748  True\n",
      "3  foo    one  0.496714 -0.469474 -1.012831  None\n",
      "4  bar  three  1.523030 -0.465730 -1.412304  None\n",
      "5  foo    two  0.647689 -0.463418 -0.908024  None\n",
      "6  foo    two -0.234153  0.241962  1.465649  None\n",
      "7  bar    one -0.138264  0.542560  0.314247  True\n"
     ]
    }
   ],
   "source": [
    "# To not modify the original df but assign the result to a new variable\n",
    "#  df_new = df.reset_index(drop=True)\n",
    "# To do it in place\n",
    "#  df.reset_index(drop=True, inplace=True)\n",
    "\n",
    "df_new = df.reset_index(drop=True)\n",
    "print(df_new)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8aca82eb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     A      B         C         D         E     F\n",
      "5  bar    two -0.234137 -1.913280 -0.225776  True\n",
      "6  foo    one  1.579213 -1.724918  0.067528  None\n",
      "7  foo  three  0.767435 -0.562288 -1.424748  True\n",
      "0  foo    one  0.496714 -0.469474 -1.012831  None\n",
      "3  bar  three  1.523030 -0.465730 -1.412304  None\n",
      "2  foo    two  0.647689 -0.463418 -0.908024  None\n",
      "4  foo    two -0.234153  0.241962  1.465649  None\n",
      "1  bar    one -0.138264  0.542560  0.314247  True\n"
     ]
    }
   ],
   "source": [
    "# confirm that the original df remains intact\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "30987a76",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     A      B         C         D         E     F\n",
      "0  bar    two -0.234137 -1.913280 -0.225776  True\n",
      "1  foo    one  1.579213 -1.724918  0.067528  None\n",
      "2  foo  three  0.767435 -0.562288 -1.424748  True\n",
      "3  foo    one  0.496714 -0.469474 -1.012831  None\n",
      "4  bar  three  1.523030 -0.465730 -1.412304  None\n",
      "5  foo    two  0.647689 -0.463418 -0.908024  None\n",
      "6  foo    two -0.234153  0.241962  1.465649  None\n",
      "7  bar    one -0.138264  0.542560  0.314247  True\n"
     ]
    }
   ],
   "source": [
    "# Modify it inplace now.\n",
    "df.reset_index(drop=True, inplace=True)\n",
    "print(df)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
playground/transclude_jupyter_notebook_from_github.txt · Last modified: 2022/03/19 02:16 by admin