[英]Logical MultiIndexing in Pandas
Is there a way to extract all values of B
for index One
where C
is greater than zero? 有没有一种方法来提取的所有值B
的指数One
,其中C
大于零? So I want to extract the values -0.22
and -1.21
. 所以我想提取值-0.22
和-1.21
。
import numpy as np
import pandas as pd
arrays =[np.array(['One','One','One','Two','Two','Two']),np.array(['A','B','C','A','B','C'])]
df = pd.DataFrame(np.random.randn(6,5),index=arrays)
df
0 1 2 3 4
One A -0.908680 0.031505 -0.087090 -0.039527 0.221196
B 1.010757 1.272553 -0.220535 -1.216996 -0.122108
C -0.781714 -1.830215 0.584311 0.010987 -0.050355
Two A -0.331269 0.410596 0.569802 1.455710 0.377796
B 0.079330 -2.538031 -1.665904 0.477257 0.500805
C -0.388749 2.188289 -1.465292 0.594870 -0.031983
You can create mask
and then use loc
by mask
: 您可以创建mask
,然后通过mask
使用loc
:
import numpy as np
import pandas as pd
np.random.seed(1)
arrays = [np.array(['One','One','One','Two','Two','Two']),
np.array(['A','B','C','A','B','C'])]
df = pd.DataFrame(np.random.randn(6,5),index=arrays)
print (df)
0 1 2 3 4
One A 1.624345 -0.611756 -0.528172 -1.072969 0.865408
B -2.301539 1.744812 -0.761207 0.319039 -0.249370
C 1.462108 -2.060141 -0.322417 -0.384054 1.133769
Two A -1.099891 -0.172428 -0.877858 0.042214 0.582815
B -1.100619 1.144724 0.901591 0.502494 0.900856
C -0.683728 -0.122890 -0.935769 -0.267888 0.530355
idx = pd.IndexSlice
mask = (df.loc[idx['One', 'C'],:]) > 0
print (mask)
0 True
1 False
2 False
3 False
4 True
Name: (One, C), dtype: bool
print (df.loc[idx['One', 'B'], mask])
0 -2.301539
4 -0.249370
Name: (One, B), dtype: float64
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.