【问题标题】:Using `.at` or `.iat` scalar access methods and boolean indexing on pandas DataFrames在 pandas DataFrames 上使用 `.at` 或 `.iat` 标量访问方法和布尔索引
【发布时间】:2017-07-04 11:06:52
【问题描述】:

我发现了用于快速标量索引的 pandas DataFrames 的 .at.iat 方法。

http://pandas.pydata.org/pandas-docs/stable/indexing.html#fast-scalar-value-getting-and-setting

有没有办法将它们与布尔索引结合起来?

In [1]: import pandas as pd

In [2]: data = {
   ...:   "A": [1, 2],
   ...:   "B": [3, 4]
   ...: }

In [3]: df = pd.DataFrame(data)

In [4]: df.index = ["x", "y"]

In [5]: df
Out[5]: 
   A  B
x  1  3
y  2  4

In [6]: df.ix[df.A == 1, "B"]
Out[6]: 
x    3
Name: B, dtype: int64

In [7]: df.ix[df.A == 1, "B"].values[0]
Out[7]: 3

In [8]: df.at["x", "B"]
Out[8]: 3

In [9]: df.at[df.A == 1, "B"]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-e2b7f23503ca> in <module>()
----> 1 df.at[df.A == 1, "B"]

/home/jlcano/.miniconda3/envs/py36/lib/python3.6/site-packages/pandas/core/indexing.py in __getitem__(self, key)
   1663 
   1664         key = self._convert_key(key)
-> 1665         return self.obj.get_value(*key, takeable=self._takeable)
   1666 
   1667     def __setitem__(self, key, value):

/home/jlcano/.miniconda3/envs/py36/lib/python3.6/site-packages/pandas/core/frame.py in get_value(self, index, col, takeable)
   1898         series = self._get_item_cache(col)
   1899         engine = self.index._engine
-> 1900         return engine.get_value(series.get_values(), index)
   1901 
   1902     def set_value(self, index, col, value, takeable=False):

pandas/index.pyx in pandas.index.IndexEngine.get_value (pandas/index.c:3557)()

pandas/index.pyx in pandas.index.IndexEngine.get_value (pandas/index.c:3240)()

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:3986)()

TypeError: 'x     True
y    False
Name: A, dtype: bool' is an invalid key

这是我找到的最简单的解决方案:

In [10]: df.at[df[df.A == 1].index.tolist()[0], "B"]
Out[10]: 3

【问题讨论】:

    标签: python-3.x pandas indexing


    【解决方案1】:

    IIUC 你可以这样做:

    In [131]: df
    Out[131]:
       A  B
    x  1  3
    y  2  4
    z  1  5
    
    In [132]: df.at[(df.A == 1).idxmax(), 'B']
    Out[132]: 3
    

    【讨论】:

      猜你喜欢
      • 2015-06-02
      • 1970-01-01
      • 2013-01-21
      • 2015-04-29
      • 1970-01-01
      • 2016-02-04
      • 2014-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多