Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

I came across the below line of code, which gives an error when '.index' is not present in it.

print(df.drop(df[df['Quantity'] == 0].index).rename(columns={'Weight': 'Weight (oz.)'}))

What is the purpose of '.index' while using drop in pandas?

1 Answer

0 votes
by (41.4k points)
edited by

You can use drop with index:

   A  B   C   D

0  0  1   2  3

1  4  5   6  7

2  8  9  10  11

df.drop([0, 1]) # Here 0 and 1 are the index of the rows

Output:

   A  B   C   D

2  8  9  10  11

Here, In this case it will drop the first 2 rows. 

Using .index, you find the rows where Quantity=0 and retrieve their index.

Enroll in MSc in Data Science in UK to enhance to your knowledge in Data Science!

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...