[英]How to locate the Occurrences of a Specific value in pandas Dataframe
I want to locate a specific value which can occur in multiple columns.我想找到一个可能出现在多列中的特定值。 In my case it's "False".
就我而言,它是“错误的”。 I know how to search "False" in individual columns as
我知道如何在各个列中搜索“False”
df.loc[df['column1']==False]
df.loc[df['column2']==False]
Is there a way to find all at once?有没有办法一次性找到?
Unnamed: 0 Incident ID CR Number Victims Zip Code Address Number Latitude Longitude Year Month Day
0 False False True True True True True True True True True
1 False True False False True True True True True True True
2 True True True True True True False True True True True
3 True True False True True True True True True True True
4 True True True True True True True True False True True
I want to see their locations.Hopefully something like我想看看他们的位置。希望像
df.applymap.loc(False)
If you want to get the indices stack
, then slice:如果要获取索引
stack
,请切片:
s = df.stack()
s[s.eq(False)].index
Or if you only have True
/ False
:或者,如果您只有
True
/ False
:
s[~s].index
In one line:在一行中:
df.stack().loc[lambda s: ~s].index
If you want to assign value(s) on the False
positions, a simple boolean indexing will do:如果要在
False
位置上分配值,一个简单的 boolean 索引就可以了:
df[df.eq(False)] = 'X'`
# or, if only True/False in the df
df[~df] = 'X'`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.