Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

I saw this code in someone's iPython notebook, and I'm very confused as to how this code works. As far as I understood, pd.loc[] is used as a location-based indexer where the format is:

df.loc[index,column_name]

However, in this case, the first index seems to be a series of boolean values. Could someone please explain to me how this selection works. I tried to read through the documentation but I couldn't figure out an explanation. Thanks!

iris_data.loc[iris_data['class'] == 'versicolor', 'class'] = 'Iris-versicolor'

enter image description here 

1 Answer

0 votes
by (106k points)

The pd.DataFrame.loc can take one or two indexers. So if you will provide only one indexer then it applies to the index of the dataframe and the missing indexer is assumed to represent all columns. So the following two examples are equivalent.

  1. df.loc[i]

  2. df.loc[i, :]

Where : is used to represent all columns.

To know more about this you can have a look at the following video tutorial:-

Related questions

0 votes
1 answer
asked Jul 10, 2019 in Data Science by sourav (17.6k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...