Rolex Pearlmaster Replica
  Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
This article is part of in the series
Published: Tuesday 27th December 2022

The tabular structures in the Python pandas library are also termed DataFrames. 

These structures represent the rows and columns using labels. A row label is called an index, and a column label is called a column index/header.

DataFrames are created by filtering and manipulating large datasets. But these processed DataFrames have the same index as their original datasets. This calls for resetting the index of the DataFrame.

Resetting the index is also helpful:

  • In the pre-processing stage, when dropping missing values or filtering data. Besides making the DataFrame smaller, it also jumbles the index.
  • When the index labels don't provide much information about the data.
  • When the index needs to be treated as a common DataFrame column.

You can use the reset_index() method in pandas to reset the index in a DataFrame. Doing this converts the original index of the DataFrame into a column.

We'll walk you through using the method to reset Pandas DataFrames in this post.

Basics of pandas.reset_index

Besides resetting the index of a DataFrame to a default one, the reset_index() method is also helpful in removing one or more levels of a DataFrame with a MultiIndex.

The syntax of the method is:

pandas.reset_index(level=None, drop=False, inplace=False, col_level=0, col_fill= ”)

 

Where the parameters have the following meanings:

Parameter

Acceptable Data Types

Default Value

Description

level

int, str, tuple, list

None

Removes all levels by default. If levels are mentioned, it removes those.

drop

bool

False

Adds the old index into the DataFrame by default. It does not add it if value is changed to True.

inplace

bool

False

Carries out the changes in the current DataFrame object

col_level

int, str

0

Determines which level the labels need to be inserted (if multiple levels are involved). The labels are inserted into the first level (0) by default.

col_fill

object

"

It determines how the levels are named if columns have multiple levels. If the value is None, the index name is repeated.

 

The .reset_index() method returns None if inplace = True. Otherwise, the DataFrame with the new index is returned.

Resetting the Index with .reset_index()

Using the .reset_index() method to reset the index is as simple as chaining the method to the DataFrame object. 

You must begin by creating a DataFrame, like so:

import pandas as pd
import numpy as np
import random
# We're making a DataFrame with an initial index. It represents marks out of 50. 

df = pd.DataFrame({
                    'Global Finance': [44, 29, 50, 17, 36],
                    'Politics': [31, 43, 21, 42, 17],
                    'Family Enterprise': [30, 30, 16, 46, 41]
                  }, index=['Leonard', 'Brayan', 'Wendy', 'Nathaniel', 'Edwin']
                 )
df

In tabular form, the DataFrame would look like this:

 

Global Finance

Politics

Family Enterprise

Leonard

44

31

30

Brayan

29

43

30

Wendy

50

21

16

Nathaniel

71

42

46

Edwin

36

17

41

 

Resetting the index is now simply a matter of calling .reset_index(), like so:

df.reset_index()

 

The index will apply to the DataFrame as a new column named "index." It begins with zero and continues along the length of the DataFrame. It'll look like this:

 

index

Global Finance

Politics

Family Enterprise

0

Leonard

44

31

30

1

Brayan

29

43

30

2

Wendy

50

21

16

3

Nathaniel

71

42

46

4

Edwin

36

17

41

 

Persisting the Change to the DataFrame

The output you see above indicates that the DataFrame's index has been changed. However, if you run "df," you will see that the changes don't persist, and the output does not have an index. 

If you want the changes to persist, you must use the "inplace" parameter, setting its value to "True." Here's what running it looks like:

df.reset_index(inplace=True)

df

 

The output of this code will be:

 

index

Global Finance

Politics

Family Enterprise

0

Leonard

44

31

30

1

Brayan

29

43

30

2

Wendy

50

21

16

3

Nathaniel

71

42

46

4

Edwin

36

17

41

 

Resetting an Index in a DataFrame with a Named Index

If a DataFrame has a named index, that is, an index with a name, then resetting the index will lead to the named index in question becoming a column name in the DataFrame. 

Let's see how this works by first creating a DataFrame with a named index:

namedIndex = pd.Series(['Leonard', 'Brayan', 'Wendy', 'Nathaniel', 'Edwin'], name='initial_index') # Creating a series and giving it a name

df = pd.DataFrame({
                    'Global Finance': [44, 29, 50, 17, 36],
                    'Politics': [31, 43, 21, 42, 17],
                    'Family Enterprise': [30, 30, 16, 46, 41]
                  }, index=['Leonard', 'Brayan', 'Wendy', 'Nathaniel', 'Edwin']
                 ) # Creating the DataFrame, then passing the named series as the index

df

 

This DataFrame would look like this:

 

Global Finance

Politics

Family Enterprise

initial_index

     

Leonard

44

31

30

Brayan

29

43

30

Wendy

50

21

16

Nathaniel

71

42

46

Edwin

36

17

41

 

Executing df.reset_index puts the "initial_index" entry in the table a column name in the DataFrame, like so:

 

initial_index

Global Finance

Politics

Family Enterprise

0

Leonard

44

31

30

1

Brayan

29

43

30

2

Wendy

50

21

16

3

Nathaniel

71

42

46

4

Edwin

36

17

41

 

Resetting a Multi-Level Index in a DataFrame

Let's take a look at a multi-level index in a DataFrame:

# Creating a multi-level index
newIndex = pd.MultiIndex.from_tuples(
                                      [('BBA', 'Leonard'),
                                       ('BBA', 'Brayan'),
                                       ('MBA', 'Wendy'),
                                       ('MBA', 'Nathaniel'),
                                       ('BSC', 'Edwin')
                                      ],
                                  names= ['Branch', 'Name'])

# Creating multi-level columns
columns = pd.MultiIndex.from_tuples(
                                    [('subject1', 'Global Finance'),
                                     ('subject2', 'Politics'),
                                     ('subject3', 'Family Enterprise')
                                    ])

df = pd.DataFrame([
                    (45, 31, 30),
                    (29, 21, 30),
                    (50, 21, 16),
                    (17, 42, 46),
                    (36, 17, 41)      
                    ], index=newIndex, 
                    columns=columns)
df

 

The output of which is:

   

subject1

subject2

subject3

 

 

Global Finance

Politics

Family Enterprise

Branch

Name

     

BBA

Leonard

44

31

30

Brayan

29

43

30

MBA

Wendy

50

21

16

Nathaniel

71

42

46

BSC

Edwin

36

17

41

 

The Branch level maps to multiple rows, making this a multi-level index. Applying the .reset_index() function merges the levels as columns in the DataFrame. 

So, running "df.reset_index()" will do this:

     

subject1

subject2

subject3

     

Global Finance

Politics

Family Enterprise

 

Branch

Name

     

0

BBA

Leonard

44

31

30

1

BBA

Brayan

29

43

30

2

MBA

Wendy

50

21

16

3

MBA

Nathaniel

71

42

46

4

BSC

Edwin

36

17

41

 

You can also reset the index at the Branch level with the level parameter like so:

df.reset_index(level='Branch')

 

Which produces the output:

   

subject1

subject2

subject3

   

Global Finance

Politics

Family Enterprise

Name

Branch

     

Leonard

BBA

44

31

30

Brayan

BBA

29

43

30

Wendy

MBA

50

21

16

Nathaniel

MBA

71

42

46

Edwin

BSC

36

17

41

 

Latest Articles


Tags

  • Unpickling
  • array
  • sorting
  • reversal
  • Python salaries
  • list sort
  • Pip
  • .groupby()
  • pyenv global
  • NumPy arrays
  • Modulo
  • OpenCV
  • Torrent
  • data
  • int function
  • file conversion
  • calculus
  • python typing
  • encryption
  • strings
  • big o calculator
  • gamin
  • HTML
  • list
  • insertion sort
  • in place reversal
  • learn python
  • String
  • python packages
  • FastAPI
  • argparse
  • zeros() function
  • AWS Lambda
  • Scikit Learn
  • Free
  • classes
  • turtle
  • convert file
  • abs()
  • python do while
  • set operations
  • data visualization
  • efficient coding
  • data analysis
  • HTML Parser
  • circular queue
  • effiiciency
  • Learning
  • windows
  • reverse
  • Python IDE
  • python maps
  • dataframes
  • Num Py Zeros
  • Python Lists
  • Fprintf
  • Version
  • immutable
  • python turtle
  • pandoc
  • semantic kernel
  • do while
  • set
  • tabulate
  • optimize code
  • object oriented
  • HTML Extraction
  • head
  • selection sort
  • Programming
  • install python on windows
  • reverse string
  • python Code Editors
  • Pytest
  • pandas.reset_index
  • NumPy
  • Infinite Numbers in Python
  • Python Readlines()
  • Trial
  • youtube
  • interactive
  • deep
  • kernel
  • while loop
  • union
  • tutorials
  • audio
  • github
  • Parsing
  • tail
  • merge sort
  • Programming language
  • remove python
  • concatenate string
  • Code Editors
  • unittest
  • reset_index()
  • Train Test Split
  • Local Testing Server
  • Python Input
  • Studio
  • excel
  • sgd
  • deeplearning
  • pandas
  • class python
  • intersection
  • logic
  • pydub
  • git
  • Scrapping
  • priority queue
  • quick sort
  • web development
  • uninstall python
  • python string
  • code interface
  • PyUnit
  • round numbers
  • train_test_split()
  • Flask module
  • Software
  • FL
  • llm
  • data science
  • testing
  • pathlib
  • oop
  • gui
  • visualization
  • audio edit
  • requests
  • stack
  • min heap
  • Linked List
  • machine learning
  • scripts
  • compare string
  • time delay
  • PythonZip
  • pandas dataframes
  • arange() method
  • SQLAlchemy
  • Activator
  • Music
  • AI
  • ML
  • import
  • file
  • jinja
  • pysimplegui
  • notebook
  • decouple
  • queue
  • heapify
  • Singly Linked List
  • intro
  • python scripts
  • learning python
  • python bugs
  • ZipFunction
  • plus equals
  • np.linspace
  • SQLAlchemy advance
  • Download
  • No
  • nlp
  • machiine learning
  • dask
  • file management
  • jinja2
  • ui
  • tdqm
  • configuration
  • deque
  • heap
  • Data Structure
  • howto
  • dict
  • csv in python
  • logging in python
  • Python Counter
  • python subprocess
  • numpy module
  • Python code generators
  • KMS
  • Office
  • modules
  • web scraping
  • scalable
  • pipx
  • templates
  • python not
  • pytesseract
  • env
  • push
  • search
  • Node
  • python tutorial
  • dictionary
  • csv file python
  • python logging
  • Counter class
  • Python assert
  • linspace
  • numbers_list
  • Tool
  • Key
  • automation
  • website data
  • autoscale
  • packages
  • snusbase
  • boolean
  • ocr
  • pyside6
  • pop
  • binary search
  • Insert Node
  • Python tips
  • python dictionary
  • Python's Built-in CSV Library
  • logging APIs
  • Constructing Counters
  • Assertions
  • Matplotlib Plotting
  • any() Function
  • Activation
  • Patch
  • threading
  • scrapy
  • game analysis
  • dependencies
  • security
  • not operation
  • pdf
  • build gui
  • dequeue
  • linear search
  • Add Node
  • Python tools
  • function
  • python update
  • logging module
  • Concatenate Data Frames
  • python comments
  • matplotlib
  • Recursion Limit
  • License
  • Pirated
  • square root
  • website extract python
  • steamspy
  • processing
  • cybersecurity
  • variable
  • image processing
  • incrementing
  • Data structures
  • algorithm
  • Print Node
  • installation
  • python function
  • pandas installation
  • Zen of Python
  • concatenation
  • Echo Client
  • Pygame
  • NumPy Pad()
  • Unlock
  • Bypass
  • pytorch
  • zipp
  • steam
  • multiprocessing
  • type hinting
  • global
  • argh
  • c vs python
  • Python
  • stacks
  • Sort
  • algorithms
  • install python
  • Scopes
  • how to install pandas
  • Philosophy of Programming
  • concat() function
  • Socket State
  • % Operator
  • Python YAML
  • Crack
  • Reddit
  • lightning
  • zip files
  • python reduce
  • library
  • dynamic
  • local
  • command line
  • define function
  • Pickle
  • enqueue
  • ascending
  • remove a node
  • Django
  • function scope
  • Tuple in Python
  • pandas groupby
  • pyenv
  • socket programming
  • Python Modulo
  • Dictionary Update()
  • Hack
  • sdk
  • python automation
  • main
  • reduce
  • typing
  • ord
  • print
  • network
  • matplotlib inline
  • Pickling
  • datastructure
  • bubble sort
  • find a node
  • Flask
  • calling function
  • tuple
  • GroupBy method
  • Pythonbrew
  • Np.Arange()
  • Modulo Operator
  • Python Or Operator
  • Keygen
  • cloud
  • pyautogui
  • python main
  • reduce function
  • type hints
  • python ord
  • format
  • python socket
  • jupyter
  • Python is a beautiful language.