如何通过任何列中的索引值和值搜索pandas数据框

我试图选择数据,从文件读入,由值1和零表示.我希望能够从值列表中选择行,同时选择每个所选行的值为1的任何列.为了使它更复杂,我还想从值列表中选择行,其中这些行的列中的所有值都为零.这可能吗?最终,如果除了熊猫数据框架之外的其他方法可以更好地工作,我愿意尝试.

要清楚,可能会选择任何列,我不知道哪些列提前.

谢谢!

解决方法:

您可以使用all()any()ix []运算符.有关详细信息,请查看official documentationthis thread

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)]

相关文章

转载:一文讲述Pandas库的数据读取、数据获取、数据拼接、数...
Pandas是一个开源的第三方Python库,从Numpy和Matplotlib的基...
整体流程登录天池在线编程环境导入pandas和xrld操作EXCEL文件...
 一、numpy小结             二、pandas2.1为...
1、时间偏移DateOffset对象DateOffset类似于时间差Timedelta...
1、pandas内置样式空值高亮highlight_null最大最小值高亮背景...