How to search pandas data frame by index value and value in any column

风格不统一 提交于 2019-12-03 16:39:17

You can use all() any() ix[] operators. Check the official documentation, or this thread for more details

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)]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!