简体   繁体   English

从pandas中的列中提取单个值

[英]Extracting single value from column in pandas

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 ie 33. How do I get that? 但是,我只想获得B列中的值,即33.我如何得到它?

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 values of 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

Simply: df['B'][df['A'] == 23] 简单地说: df['B'][df['A'] == 23]

Thanks @Jeff. 谢谢@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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM