The pop() method

If you are familiar with lists in Python, the pop() method will ring a bell. The pop() method in series and DataFrames behaves exactly the same as it does with lists. It helps remove entire columns from DataFrames or specific rows from series. Upon being called, the pop() method returns the popped out entity (row or column).

The following snippet demonstrates the use of pop() with a series. Let's pop the item with row index 2 from the "Wins" column:

In: sample_df["Wins"].pop(2)
Out: 26

Now, let's print the Wins series:

Output of the pop method

It can be seen that index 2 is no longer present in the series. The same method can be applied to a DataFrame as well. Let's pop the Nominations column to understand this:

sample_df.pop("Nominations")

The following is the output:

Pop applied on the DataFrame

The following command is used to show the DataFrame result after popping:

sample_df

The following is the output:

DataFrame result after popping
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset