0 votes
1 view
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 (38.2k points)

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.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...