如何通过任何列中的索引值和值搜索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)]
上一篇:ABAP报表返回选择屏幕时回到上次的页签


下一篇:Java – 选择排序算法