[英]pandas check if value exists in one specific index in a MultiIndex dataframe
I have a MultiIndex
data frame called df
with 3 indexes
(Fruit, Color, Taste).我有一个名为
df
的MultiIndex
数据框,其中包含 3 个indexes
(水果、颜色、味道)。 I want to search 1 specific index
, that index
being Color
and see if the value
exists in it.我想搜索 1 个特定的
index
,该index
是Color
并查看该value
是否存在于其中。
For example: the code would look something like this.例如:代码看起来像这样。
Color
is an index
in the dataframe not just a column. Color
是 dataframe 中的一个index
,而不仅仅是一列。
if 'purple' in 'Color':
print('yes')
else:
print('no')
I only want it to search the Color
not any other indexes/columns
我只希望它搜索
Color
而不是任何其他indexes/columns
Quantity Quality
Fruit Color Taste
apple red tart 12 good
lemon yellow sour 11 average
grapes purple sweet 5 bad
lime green citrus 3 excellent
Thank you so much for your time!非常感谢您的参与!
Try xs
to grab a cross-section from the DataFrame:试试
xs
从 DataFrame 中抓取一个横截面:
df.xs('purple', level=1, drop_level=False)
Quantity Quality
Fruit Color Type
grapes purple sweet 5 bad
Just to test if it exists or not check if the cross-section is empty
or not:只是为了测试它是否存在检查横截面是否为
empty
:
df.xs('purple', level=1).empty
if not df.xs('purple', level=1).empty:
print('yes')
else:
print('no')
Alternatively reset_index
and use any
to see if there are any matches:或者
reset_index
并使用any
查看是否有任何匹配项:
df.reset_index('Color')['Color'].eq('purple').any()
True
if df.reset_index('Color')['Color'].eq('purple').any():
print('yes')
else:
print('no')
You can use this if you want a tabular output:如果你想要一个表格 output,你可以使用它:
def check(data:pd.DataFrame,l:list):
c = data.index.get_level_values("Color").isin(l)
return np.where(c,'yes','no')
df['Result'] = check(df,['purple'])
print(df)
Quantity Quality Result
Fruit Color Type
apple red tart 12 good no
lemon yellow sour 11 average no
grapes purple sweet 5 bad yes
lime green citrus 3 excellent no
You can use get_level_values
to filter.您可以使用
get_level_values
进行过滤。
if "purple" in df.index.get_level_values('Color'):
print('yes')
else:
print('no')
You can also check if the value is in index.unique(level)
.您还可以检查该值是否在
index.unique(level)
中。 This way the whole index would not need to be copied into a list.这样就不需要将整个索引复制到列表中。 It might also make the subsequent search faster.
它还可能使后续搜索更快。
if "purple" in df.index.unique(level="Color"):
print("yes")
else:
print("no")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.