Pandas - Как сбросить индекс в данном DataFrame
Опубликовано: 27 Марта, 2022
Давайте посмотрим, как сбросить индекс DataFrame после удаления некоторых строк из DataFrame.
Approach :
- Import the
Pandasmodule. - Create a DataFrame.
- Drop some rows from the DataFrame using the
drop()method. - Reset the index of the DataFrame using the
reset_index()method. - Display the DataFrame after each step.
# importing the modulesimport pandas as pdimport numpy as np # creating a DataFrameODI_runs = {"name": ["Tendulkar", "Sangakkara", "Ponting", "Jayasurya", "Jayawardene", "Kohli", "Haq", "Kallis", "Ganguly", "Dravid"], "runs": [18426, 14234, 13704, 13430, 12650, 11867, 11739, 11579, 11363, 10889]}df = pd.DataFrame(ODI_runs) # displaying the original DataFrameprint("Original DataFrame :")print(df) # dropping the 0th and the 1st indexdf = df.drop([0, 1]) # displaying the altered DataFrameprint("DataFrame after removing the 0th and 1st row")print(df) # resetting the DatFrame indexdf = df.reset_index() # displaying the DataFrame with new indexprint("Dataframe after resetting the index")print(df) |
Output :


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course