Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

reset index with pandas

import pandas as pd

# Reset index
df = df.reset_index()

# Display DataFrame
print(df)
Comment

The following code shows how to reset the index of the DataFrame and drop the old index completely:

#reset index
df.reset_index(drop=True, inplace=True)

#view updated DataFrame
print(df)

   points  assists  rebounds
0      25        5        11
1      12        7         8
2      15        7        10
3      14        9         6
4      19       12         6
5      23        9         5
6      25        9         9
7      29        4        12
Comment

df reset index

df.reset_index()
Comment

df = df.reset_index(level=0)

How to convert index of a pandas dataframe into a column
df = df.reset_index(level=0)
df['index1'] = df.index
Comment

pandas reset index

df.reset_index(
  drop=True, # to avoid the old index being added as a column
  inplace=False) # (default) return df with the new index, i.e. do not create a new object
Comment

reset index in pandas

df.reset_index(drop = True, inplace = True)
Comment

Example 1: Reset Index & Drop Old Index pandas

import pandas as pd

#define DataFrame
df = pd.DataFrame({'points': [25, 12, 15, 14, 19, 23, 25, 29],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]},
                   index=[0, 4, 3, 5, 2, 1, 7, 6])

#view DataFrame
print(df)

   points  assists  rebounds
0      25        5        11
4      12        7         8
3      15        7        10
5      14        9         6
2      19       12         6
1      23        9         5
7      25        9         9
6      29        4        12
Comment

PREVIOUS NEXT
Code Example
Python :: pyodbc ms access 
Python :: how to use selenium on default chrome python 
Python :: change graph colors python matplotlib 
Python :: opencv skip video frames 
Python :: python datetime without seconds 
Python :: add something to list python 
Python :: pygame escape key 
Python :: python read column data from text file 
Python :: WARNING: Ignoring invalid distribution -ip 
Python :: mongodb group by having 
Python :: spark add column to dataframe 
Python :: plt.savefig 
Python :: how to remove first letter of a string python 
Python :: pandas query on datetime 
Python :: find rows in dataframe from another dataframe python 
Python :: how to get discord username nextcord interactions 
Python :: Local to ISO 8601 with TimeZone information (Python 3): 
Python :: how to hide command console python 
Python :: ValueError: Shapes (None, 1) and (None, 11) are incompatible keras 
Python :: python get system information 
Python :: boxplot pandas 
Python :: TinyDB 
Python :: isinstance float or int 
Python :: root number in python 
Python :: check nan values in a np array 
Python :: get os environment python 
Python :: How to Convert Strings to Datetime in Pandas DataFrame 
Python :: sns palette 
Python :: datetimes to day of year python 
Python :: python matplotlib pyplot 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =