...

/

Filtering and Indexing Operations

Filtering and Indexing Operations

Explore some of the operations to change the index.

Renaming an index

In this example, we’ll use the rename method to update the index values. This method accepts a function that takes the current value and returns a new value. Here we’ll use the initial of the president’s first name:

Press + to interact
def name_to_initial(val):
names = val.split()
return ' '.join([f'{names[0][0]}.', *names[1:]])
print(pres
.set_index('President')
.rename(name_to_initial)
)

Resetting the index

If we want a monotonically increasing integer index for a DataFrame, we’ll use the reset_index method:

Press + to interact
print(pres
.set_index('President')
.reset_index()
)
...