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

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

问题描述

我在某人的 iPython notebook 中看到了这段代码,我很困惑这段代码是如何工作的.据我了解,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, :]

其中 : 用于表示所有列.

Where : is used to represent all columns.

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

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

现在我们可以关注 ij 可以假设的值的类型.让我们使用以下数据帧 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 已经写成 ij 可以是

loc has been written such that i and j can be

  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 对象.如果我们为 ij 传递一个数组,我们可以操纵它返回一个数据帧,并且该数组可能只是一个单值数组.

      • 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
        

      • 布尔数组,其元素为 TrueFalse,并且其长度与相应索引的长度匹配.在这种情况下,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''versicolor'
        4. 的所有行的 class'
        5. 与赋值运算符一起使用时:

        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'
        

        我们为 'class' 列中的所有元素分配 'Iris-versicolor',其中 'class''versicolor'

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

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

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