Pandas: How to use LocIndexer?

I am trying to figure out how to use a Locindexer object to select a subset of a DataFrame.

for instance

var = df.loc(df['rating'] == 4)

Returns as

pandas.core.indexing._LocIndexer

How to use LocIndexer to select a subset of my frame?

+5
source share
3 answers

You call it a function. For indexing you use [].

 df.loc[df['rating'] == 4] 

will return a row / rows in which the column value is 4.

+7
source

You should have no problem choosing a subset using the following code.

 subset = df.loc[(df['rating'] == 4)] 
+1
source

Also, if you selected a line with:

 subset = df.loc[(df['rating'] == 4)] 

you can get a special column of this row with

1.

 df.loc[(df['rating'] == 4)]["column] 

2.

 subset["column"] 
0
source

Source: https://habr.com/ru/post/1265758/


All Articles