pandas 从熊猫列中提取单个值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/21923524/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extracting single value from column in pandas
提问by user308827
I have a simple pandas question regarding extracting a single column value
我有一个关于提取单列值的简单Pandas问题
df = DataFrame({'A' : [15,56,23,84], 'B' : [10,20,33,25]})
df
     A    B
0    15   10
1    56   20
2    23   33
3    84   55
x = df[df['A'] == 23]
x
outputs
输出
    A    B
2  23    33
However, I only want to get the value in column B i.e. 33. How do I get that?
但是,我只想获取 B 列中的值,即 33。如何获取?
回答by Andy Hayden
My preferred way is Jeff's using loc (it's generally good practice to avoid working on copies, especially if you might later do assignment).
我的首选方式是 Jeff 使用 loc (避免处理副本通常是一种很好的做法,尤其是如果您以后可能会进行分配)。
You can eek some more performance by not creating a Series for the boolean mask, just a numpy array:
你可以通过不为布尔掩码创建一个系列来获得更多的性能,只是一个 numpy 数组:
df = pd.DataFrame(np.random.randint(1, 100, 2000).reshape(-1, 2),
                  columns=list('AB'))
In [21]: %timeit df.loc[df.A == 23, 'B']
1000 loops, best of 3: 532 μs per loop
In [22]: %timeit df['B'][df.A == 23]
1000 loops, best of 3: 432 μs per loop
In [23]: %timeit df.loc[df.A.values == 23, 'B']  # preferred
1000 loops, best of 3: 294 μs per loop
In [24]: %timeit df['B'].loc[df.A.values == 23]
1000 loops, best of 3: 197 μs per loop
I'm not sure why this is so slow tbh, maybe this usecase could be improved...? (I'm not sure where the the extra 100us is spent)...
我不确定为什么这太慢了,也许这个用例可以改进......?(我不确定额外的 100us 花在哪里了)...
However, if you are just interested in the valuesof B and not their corresponding index (and the subframe) it's much faster just to use the numpy arrays directly:
但是,如果您只对B的值感兴趣,而不对它们相应的索引(和子帧)感兴趣,那么直接使用 numpy 数组会更快:
In [25]: %timeit df.B.values[df.A.values == 23]
10000 loops, best of 3: 60.3 μs per loop
回答by CT Zhu
Simply: df['B'][df['A'] == 23]
简单地: df['B'][df['A'] == 23]
Thanks @Jeff.
谢谢@杰夫。
And the speed comparisons:
以及速度比较:
In [30]:
%timeit df['B'][df['A'] == 23].values
1000 loops, best of 3: 813 μs per loop
In [31]:
%timeit df.loc[df['A'] == 23, 'B']
1000 loops, best of 3: 976 μs per loop

