pandas: Reset index#

import pandas as pd

Loading data into Pandas DataFrame#

The dataset is from Kaggle - Pokemon.

data = pd.read_csv('data/Pokemon.csv')
data

Hide code cell output

# Name Type 1 Type 2 Total HP Attack Defense Sp. Atk Sp. Def Speed Generation Legendary
0 1 Bulbasaur Grass Poison 318 45 49 49 65 65 45 1 False
1 2 Ivysaur Grass Poison 405 60 62 63 80 80 60 1 False
2 3 Venusaur Grass Poison 525 80 82 83 100 100 80 1 False
3 3 VenusaurMega Venusaur Grass Poison 625 80 100 123 122 120 80 1 False
4 4 Charmander Fire NaN 309 39 52 43 60 50 65 1 False
... ... ... ... ... ... ... ... ... ... ... ... ... ...
795 719 Diancie Rock Fairy 600 50 100 150 100 150 50 6 True
796 719 DiancieMega Diancie Rock Fairy 700 50 160 110 160 110 110 6 True
797 720 HoopaHoopa Confined Psychic Ghost 600 80 110 60 150 130 70 6 True
798 720 HoopaHoopa Unbound Psychic Dark 680 80 160 60 170 130 80 6 True
799 721 Volcanion Fire Water 600 80 110 120 130 90 70 6 True

800 rows × 13 columns

In the cell below, you can see the index is not consecutive (the leftmost column). We can reset it with the reset_index() method. By default, it adds the old index as a new column. If you do not want that, use the drop=True parameter.

new_data = data.loc[(data['Type 1'] == 'Grass') | (data['Type 2'] == 'Poison')][0::10].loc[data['HP'] > 50].head(3)
new_data

Hide code cell output

# Name Type 1 Type 2 Total HP Attack Defense Sp. Atk Sp. Def Speed Generation Legendary
50 45 Vileplume Grass Poison 490 75 80 85 110 90 50 1 False
101 94 Gengar Ghost Poison 500 60 65 60 130 75 110 1 False
197 182 Bellossom Grass NaN 490 75 80 95 90 100 50 2 False
new_data.reset_index(drop=True, inplace=True)
new_data

Hide code cell output

# Name Type 1 Type 2 Total HP Attack Defense Sp. Atk Sp. Def Speed Generation Legendary
0 45 Vileplume Grass Poison 490 75 80 85 110 90 50 1 False
1 94 Gengar Ghost Poison 500 60 65 60 130 75 110 1 False
2 182 Bellossom Grass NaN 490 75 80 95 90 100 50 2 False