pandas 如果值出现在熊猫数据框的任何列中,如何打印行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24664913/
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
How to print rows if values appear in any column of pandas dataframe
提问by KieranPC
I would like to print all rows of a dataframe where I find the value '-' in any of the columns. Can someone please explain a way that is better than those described below?
我想打印数据帧的所有行,其中在任何列中找到值“-”。有人可以解释一种比下面描述的方法更好的方法吗?
This Q&Aalready explains how to do so by using boolean indexing but each column needs to be declared separately:
这个问答已经解释了如何通过使用布尔索引来做到这一点,但每一列都需要单独声明:
print df.ix[df['A'].isin(['-']) | df['B'].isin(['-']) | df['C'].isin(['-'])]
I tried the following but I get an error 'Cannot index with multidimensional key':
我尝试了以下操作,但出现错误“无法使用多维键建立索引”:
df.ix[df[df.columns.values].isin(['-'])]
So I used this code but I'm not happy with the separate printing for each column tested because it is harder to work with and can print the same row more than once:
所以我使用了这段代码,但我对测试的每一列的单独打印不满意,因为它更难处理并且可以多次打印同一行:
import pandas as pd
d = {'A': [1,2,3], 'B': [4,'-',6], 'C': [7,8,'-']}
df = pd.DataFrame(d)
for i in range(len(d.keys())):
temp = df.ix[df.iloc[:,i].isin(['-'])]
if temp.shape[0] > 0:
print temp
Output looks like this:
输出如下所示:
A B C
1 2 - 8
[1 rows x 3 columns]
A B C
2 3 6 -
[1 rows x 3 columns]
Thanks for your advice.
谢谢你的建议。
采纳答案by DSM
Alternatively, you could do something like df[df.isin(["-"]).any(axis=1)], e.g.
或者,你可以做类似的事情df[df.isin(["-"]).any(axis=1)],例如
>>> df = pd.DataFrame({'A': [1,2,3], 'B': ['-','-',6], 'C': [7,8,9]})
>>> df.isin(["-"]).any(axis=1)
0 True
1 True
2 False
dtype: bool
>>> df[df.isin(["-"]).any(axis=1)]
A B C
0 1 - 7
1 2 - 8
(Note I changed the frame a bit so I wouldn't get the axes wrong.)
(注意我稍微改变了框架,所以我不会弄错轴。)
回答by behzad.nouri
you can do:
你可以做:
>>> idx = df.apply(lambda ts: any(ts == '-'), axis=1)
>>> df[idx]
A B C
1 2 - 8
2 3 6 -
or
或者
lambda ts: '-' in ts.values
note that inlooks into the index not the values, so you need .values
请注意,in查看索引而不是值,因此您需要.values

