在python中使用.loc进行选择 [英] Selection with .loc in python

查看:611
本文介绍了在python中使用.loc进行选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在某人的iPython笔记本中看到了这段代码,我对此代码的工作原理感到非常困惑。据我所知,pd.loc []用作基于位置的索引器,格式为:

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'

推荐答案

pd.DataFrame.loc 可以使用一个或两个索引器。对于帖子的其余部分,我将代表第一个索引器为 i ,第二个索引器为 j

pd.DataFrame.loc can take one or two indexers. For the rest of the post, I'll represent the first indexer as i and the second indexer as j.

如果只提供了一个索引器,它将应用于数据帧的索引,并且假定丢失的索引器代表所有列。所以以下两个例子是等价的。

If only one indexer is provided, 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,:]

  1. df.loc[i]
  2. df.loc[i, :]

其中用于表示所有列。

如果两个索引器都存在, i 引用索引值, j 引用列值。

If both indexers are present, i references index values and j references column values.

现在我们可以专注于什么类型的值 i j 可以假设。让我们使用以下数据框 df 作为示例:

Now we can focus on what types of values i and j can assume. Let's use the following dataframe df as our example:

    df = pd.DataFrame([[1, 2], [3, 4]], index=['A', 'B'], columns=['X', 'Y'])

loc 已经写成 i j 可以


  1. 标量应该是相应索引对象中的值

  1. scalars that should be values in the respective index objects

df.loc['A', 'Y']

2


  • 数组其元素是也是相应索引对象的成员(注意我传递给 loc 的数组的顺序是否受到尊重

  • arrays whose elements are also members of the respective index object (notice that the order of the array I pass to loc is respected

    df.loc[['B', 'A'], 'X']
    
    B    3
    A    1
    Name: X, dtype: int64
    




    • 注意返回对象的维数传递数组。 i 是一个数组,如上所述, loc 返回一个对象其中返回具有这些值的索引。在这种情况下,因为 j 是标量, loc 返回 pd.Series 对象。如果我们传递 i j 的数组,我们可以操纵它来返回数据帧,并且数组可以我只是一个单值数组。

      • Notice the dimensionality of the return object when passing arrays. i is an array as it was above, loc returns an object in which an index with those values is returned. In this case, because j was a scalar, loc returned a pd.Series object. We could've manipulated this to return a dataframe if we passed an array for i and j, and the array could've have just been a single value'd array.

        df.loc[['B', 'A'], ['X']]
        
           X
        B  3
        A  1
        


      • 布尔数组,其元素为 True False 且其长度与相应索引的长度相匹配。在这种情况下, loc 只是抓取布尔数组为 True 的行(或列)。

        boolean arrays whose elements are True or False and whose length matches the length of the respective index. In this case, loc simply grabs the rows (or columns) in which the boolean array is True.

        df.loc[[True, False], ['X']]
        
           X
        A  1
        







        除了可以传递给 loc 的索引器外,它还可以让您进行分配。现在我们可以细分您提供的代码行。


        In addition to what indexers you can pass to loc, it also enables you to make assignments. Now we can break down the line of code you provided.

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




        1. iris_data ['class'] =='versicolor'返回一个布尔数组。

        2. class 是一个标量,表示列对象中的值。

        3. iris_data.loc [iris_data ['class'] =='versicolor','class' ] 返回 pd.Series 对象,该对象包含所有行的'class'列其中'class''versicolor'

        4. 使用时使用赋值运算符:

        1. iris_data['class'] == 'versicolor' returns a boolean array.
        2. class is a scalar that represents a value in the columns object.
        3. iris_data.loc[iris_data['class'] == 'versicolor', 'class'] returns a pd.Series object consisting of the 'class' column for all rows where 'class' is 'versicolor'
        4. When used with an assignment operator:

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

        我们分配'Iris-versicolor''class'中的所有元素其中'class' wa s 'versicolor'

        We assign 'Iris-versicolor' for all elements in column 'class' where 'class' was 'versicolor'

        这篇关于在python中使用.loc进行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆