Python: Pandas pandas.DataFrame.locPython: Pandas pandas.DataFrame.locPython: Pandas pandas.DataFrame.locPython: Pandas pandas.DataFrame.loc
  • 首页
  • 博客
  • 书签
  • 文件
  • 分析
  • 登录

Python: Pandas pandas.DataFrame.loc

发表 admin at 2023年8月24日
类别
  • Python
标签

pandas.DataFrame attribute: loc

Access a group of rows and columns by label(s) or a boolean array.

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.loc.html

在下面的代码中

import numpy as np
import pandas as pd

def mklbl(prefix, n):
    return ["%s%s" % (prefix, i) for i in range(n)]

miindex = pd.MultiIndex.from_product(
    [mklbl("A", 4), mklbl("B", 2), mklbl("C", 4), mklbl("D", 2)]
)

micolumns = pd.MultiIndex.from_tuples(
    [("a", "foo"), ("a", "bar"), ("b", "foo"), ("b", "bah")], names=["lvl0", "lvl1"]
)

dfmi = (
    pd.DataFrame(
        np.arange(len(miindex) * len(micolumns)).reshape(
            (len(miindex), len(micolumns))
        ),
        index=miindex,
        columns=micolumns,
    )
    .sort_index()
    .sort_index(axis=1)
)

df1 = dfmi.loc["A1", (slice(None), "foo")]
df2 = dfmi.loc(axis=0)["A0":"A2", :, ["C1", "C3"]]

print("df1:", "\n", df1)
print("df2:", "\n", df2)

运行结果:

df1: 
lvl0        a    b
lvl1      foo  foo
B0 C0 D0   64   66
      D1   68   70
   C1 D0   72   74
      D1   76   78
   C2 D0   80   82
      D1   84   86
   C3 D0   88   90
      D1   92   94
B1 C0 D0   96   98
      D1  100  102
   C1 D0  104  106
      D1  108  110
   C2 D0  112  114
      D1  116  118
   C3 D0  120  122
      D1  124  126

df2: 
lvl0           a         b     
lvl1         bar  foo  bah  foo
A0 B0 C1 D0    9    8   11   10
         D1   13   12   15   14
      C3 D0   25   24   27   26
         D1   29   28   31   30
   B1 C1 D0   41   40   43   42
         D1   45   44   47   46
      C3 D0   57   56   59   58
         D1   61   60   63   62
A1 B0 C1 D0   73   72   75   74
         D1   77   76   79   78
      C3 D0   89   88   91   90
         D1   93   92   95   94
   B1 C1 D0  105  104  107  106
         D1  109  108  111  110
      C3 D0  121  120  123  122
         D1  125  124  127  126
A2 B0 C1 D0  137  136  139  138
         D1  141  140  143  142
      C3 D0  153  152  155  154
         D1  157  156  159  158
   B1 C1 D0  169  168  171  170
         D1  173  172  175  174
      C3 D0  185  184  187  186
         D1  189  188  191  190

分别使用了loc[]和loc()的形式。

pandas.DataFrame.loc作为属性,为什么既可以使用loc[]切片的形式,又可以使用loc()方法的形式呢?
在pandas的代码中,可以看到loc属性实际上是一个方法,访问loc属性时,返回了一个class _LocIndexer(_LocationIndexer)实例:

    @property
    def loc(self) -> _LocIndexer:
        return _LocIndexer("loc", self)

类_LocIndexer继承了类_LocationIndexer, 查看类_LocationIndexer的代码:

class _LocationIndexer(NDFrameIndexerBase):
    ...

    @final
    def __call__(self: _LocationIndexerT, axis: Axis | None = None) -> _LocationIndexerT:
        ...

    @final
    def __getitem__(self, key):
        ...

类_LocationIndexer包含了__call__和__getitem__方法。
当访问loc[]时,会调用__getitem__方法,访问loc()时,会调用__call__方法。

发表回复 取消回复

要发表评论,您必须先登录。

类别

  • Cat
  • Python
  • Django
  • Database
  • Html/CSS
  • JavaScript
  • Vue
  • RegExp
  • Maths/AI
  • PHP/Wordpress
  • Practice
  • Linux
  • Windows
  • Android
  • NAS
  • Software
  • Hardware
  • Network
  • SEO
  • English
  • Games
  • Recipes
  • General
  • Memorandum
  • Essays
  • 未分类

归档

©2015-2023 艾丽卡 Blog support@alaica.com
      ajax-loader