In Pandas, you can reset the index of a Series using the reset_index()
method. This function resets the index to the default integer index and converts the original index to a new column in the DataFrame.
In this article, I will explain the reset_index()
function and using its syntax, parameters, and usage how we can reset the index of a DataFrame or Series. When applied to a Series, it returns a DataFrame with the original values of the Series as a new column, and a default sequential index.
Key Points –
- The
reset_index()
function resets the index of a Series, converting the current index into a column and reindexing the Series with a default integer index. - The
drop
parameter controls whether to drop the current index or not. Settingdrop=True
will drop the current index, anddrop=False
(default) will keep the current index in the DataFrame as a regular column. - When
reset_index()
is applied to a Series, it returns a DataFrame with two columns: one for the original index (by default named “index”) and one for the Series values. Settingdrop=True
will return a DataFrame with only one column containing the Series values, without the index. reset_index()
also allows for customizing the names of the columns in the resulting DataFrame. You can specify the name of the index column using thename
parameter, and you can rename the index column itself using theindex
parameter.
Pandas Series reset_index() Introduction
Following is the syntax of the pandas series reset_index() function.
# Syntax of Series reset_index() function
Series.reset_index(level=None, drop=False, name=None, inplace=False)
Parameters of the Series reset_index()
Following are the parameters of the Series reset_index() function.
level
(optional) – int, str, tuple, or list, defaultNone
. For aMultiIndex
, level(s) to remove from the index. IfNone
, all levels are removed.drop
(optional) – bool, defaultFalse
. Do not insert the index into the DataFrame columns. This means the old index is removed entirely and not added as a column.name
(optional) – object, defaultNone
. The name to use for the column containing the original Series values. This is only relevant when the Series is reset to a DataFrame.inplace
(optional) – bool, defaultFalse
. IfTrue
, do the operation inplace and returnNone
.
Return Value
It returns the reset_index()
function in Pandas when applied to a Series is a DataFrame. This DataFrame will have two columns: one for the original index (by default named “index”) and one for the values of the Series.
Create Pandas Series
Pandas Series can be created in several ways by using Python lists & dictionaries, below example create a Series from a dictionary. To use Pandas first, you need to import using import pandas as pd
.
import pandas as pd
# Create Pandas Series
Courses = {'Spark': 20000, 'PySpark': 15000, 'Java': 10000}
series = pd.Series(Courses)
print("Original Series:\n",series)
Yields below output.
The reset_index()
function is used to reset the index of a DataFrame or Series. When you manipulate a DataFrame or Series, it’s possible to end up with an index that doesn’t reflect the actual position of the data. The reset_index()
function helps in such situations by moving the current index to a new column and resetting the index to the default integer index.
# Reset the index
result = series.reset_index()
print("Reset Series:\n",result)
In the above examples, the index of the original Series has been moved to a new column called index
, and the default integer index has been applied to the Series. This example yields the below output.
Resetting the Index of a Series with Custom Column Names
To reset the index of a Pandas Series with custom column names, you can use the reset_index()
function along with the name
parameter to specify the name of the column containing the Series values.
# Resetting the index with custom column names
result = series.reset_index(name='value', drop=True)
print("Reset Series with custom column names:\n",result)
# Output:
# Reset Series with custom column names:
# value
# 0 20000
# 1 15000
# 2 10000
# dtype: int64
In the above example, the reset_index()
function resets the index of the Series and renames the column containing the Series values to value
using the name
parameter. The drop=True
parameter is used to drop the old index. As a result, the Series is converted into a DataFrame with a single column named value
.
Resetting the Index of a Series and Keeping the Old Index
Alternatively, to reset the index of a Pandas Series while keeping the old index as a column in the resulting DataFrame, you can set the drop
parameter of the reset_index()
function to False
.
# Resetting the index and keeping the old index
result = series.reset_index(name='value', drop=False)
print("Reset Series with old index as a column:\n",result)
# Output:
# Reset Series with old index as a column:
# index value
# 0 Spark 20000
# 1 PySpark 15000
# 2 Java 10000
In the above example, the reset_index()
function resets the index of the Series and retains the old index as a column named index
in the resulting DataFrame. The drop=False
parameter ensures that the old index is kept.
Resetting the Index of a Series and Preserving the Original Index
To reset the index of a Pandas Series while preserving the original index, you can use reset_index()
function without specifying any additional parameters. By default, reset_index()
will move the original index to a new column in the resulting DataFrame.
# Resetting the index and preserving the original index
result = series.reset_index(drop=True)
print("Reset Series with preserved original index:\n",result)
# Output:
# Reset Series with preserved original index:
# 0 20000
# 1 15000
# 2 10000
# dtype: int64
In the above example, the reset_index()
function resets the index of the Series and moves the original index to a new column in the resulting DataFrame. Setting drop=True
ensures that the original index is not retained as a separate column in the DataFrame.
Resetting the Index of a Series and Handling Missing Values
Similarly, to reset the index of a Pandas Series and handle missing values, you can first reset the index using reset_index()
, and then handle the missing values as needed.
import pandas as pd
import numpy as np
# Create Pandas Series
Courses = {'Spark': 20000, 'PySpark': None, 'Java': 10000}
series = pd.Series(Courses)
# Resetting the index
result = series.reset_index(drop=True)
print("Reset Series:\n",result)
# Output:
# Reset Series:
# 0 20000.0
# 1 NaN
# 2 10000.0
# dtype: float64
# Handling missing values
result = result.dropna()
print("Reset Series with missing values removed:\n",result)
# Resetting the index and handling missing values
result = series.reset_index(drop=True).dropna()
print("Reset Series with missing values removed:\n",result)
# Output:
# Reset Series with missing values removed:
# 0 20000.0
# 2 10000.0
# dtype: float64
In the above example, the reset_index()
function is used to reset the index of the Series, and the drop=True
parameter is used to drop the original index. The resulting Series, result
, contains missing values represented as NaN
. Then, dropna()
is used to remove the rows containing missing values, resulting in a Series without missing values.
Resetting the Index of a Series with a Specified fill Value for Missing Values
You can reset the index of a Pandas Series with a specified fill value for missing values, you can first reset the index using reset_index()
, and then use the fillna()
method to replace the missing values with the specified fill value.
# Filling missing values with a specified fill value
fill_value = -1
result = result.fillna(fill_value)
print("Reset Series with missing values filled:\n",result)
# Output:
# Reset Series with missing values filled:
# 0 20000.0
# 1 -1.0
# 2 10000.0
# dtype: float64
In the above example, the reset_index()
function is used to reset the index of the Series, and the drop=True
parameter is used to drop the original index. The resulting Series, result
, contains missing values represented as NaN
. Then, fillna()
is used to replace the missing values with the specified fill value -1
, resulting in a Series with missing values filled with -1
.
Frequently Asked Questions on Pandas Series reset_index()
The reset_index()
function in Pandas Series resets the index of the Series, converting it into a DataFrame. It moves the current index (if any) to a new column and resets the index to the default integer index.
By setting the drop
parameter to True
(which is the default behavior), reset_index()
drops the current index and returns a DataFrame with the default integer index.
To preserve the original index while resetting the index of a Pandas Series, you can set the drop
parameter of the reset_index()
function to False
. By default, the drop
parameter is set to True
, which removes the original index after resetting. Setting it to False
retains the original index as a separate column in the resulting DataFrame.
By default, the reset_index()
function in Pandas returns a new object. It does not modify the original Series inplace. If you want to modify the original Series inplace, you can use the inplace=True
parameter. Setting inplace=True
will modify the original Series, and it will return None
.
Conclusion
In this article, you have learned the reset_index()
function in pandas is used to reset the index of a DataFrame or Series. When you reset the index of a Series, it converts the index into a new column and generates a new default integer index.
Happy Learning!!
Related Articles
- Pandas Series.rolling() Function
- Pandas Series rank() Function
- Pandas Series count() Function
- Pandas Series mode() Function
- Pandas Series.clip() Function
- Pandas Series iloc[] Function
- Pandas Series round() Function
- Pandas Series.dtype() Function
- Pandas series.str.get() Function
- Pandas Series sample() Function
- Pandas Series.shift() Function
- Pandas Series.value_counts() With Examples