Python 如何按索引值和任何列中的值搜索熊猫数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26896382/
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 search pandas data frame by index value and value in any column
提问by burkesquires
I am trying to select data, read in from a file, represented by the values one and zero. I want to be able to select rows from a list of values and at the same time select for any column in which each of the selected rows has a value of one. To make it more complex I also want to select rows from a list of values where all values in a column for these rows is zero. Is this possible? Ultimately if another method besides pandas data frame would work better I would be willing to try that.
我正在尝试选择从文件中读取的数据,由值一和零表示。我希望能够从值列表中选择行,同时选择每个选定行的值为 1 的任何列。为了使它更复杂,我还想从值列表中选择行,其中这些行的列中的所有值都为零。这可能吗?最终,如果除 Pandas 数据框之外的另一种方法效果更好,我愿意尝试。
To be clear, any column may be selected and I do not know which ones ahead of time.
需要明确的是,可以选择任何列,我不知道提前选择哪些列。
Thanks!
谢谢!
采纳答案by burkesquires
You can use all()any()ix[]operators. Check the official documentation, or this threadfor more details
您可以使用all()any()ix[]运算符。查看官方文档或此线程以获取更多详细信息
import pandas as pd
import random
import numpy as np
#created a dump data as you didn't provide one
df = pd.DataFrame({'col1': [random.getrandbits(1) for i in range(10)], 'col2': [random.getrandbits(1) for i in range(10)], 'col3': [1]*10})
#You can select the value directly by using ix[] operator
row_indexer,column_indexer=3,1
print df.ix[row_indexer,column_indexer]
#You can filter the data of a specific column this way
print df[df['col1']==1]
print df[df['col2']==1]
#df.iloc to select by postion .loc to Selection by Label
#want to be able to select rows from a list of values and at the same time select for any column in which each of the selected rows has a value of one.
print df[(df.T == 1).any()]
# if you wanna filter a specific columns with a condition on rows
print df[(df['col1']==1)|(df['col2']==1)]
#To make it more complex I also want to select rows from a list of values where all values in a column for these rows is zero.
print df[(df.T == 0).all()]
# if you wanna filter a specific columns with a condition on rows
print df[(df['col1']==0) & (df['col2']==0)]

