Reputation: 19
Id 0 1 2 3 4 5
0 apple True None None None None None
1 orange False None True None None None
2 banana True None None True None None
3 guava False None None None True None
4 leeche None True None None None None
above dataframe contains boolean and None value If any of 0-5 columns have false i want to omit them from updated dataframe. considering None denotes True value , so my result should look like
Id
0 apple
2 banana
4 leeche
I am not able to understand how to get combined filter on multiple columns.
Upvotes: 1
Views: 80
Reputation: 863156
Use:
cols = ['0','1','2','3','4','5']
df = df.loc[df[cols].ne('False').all(1), ['Id']]
#if False is boolean
#df = df.loc[df[cols].ne(False).all(1), ['Id']]
print (df)
Id
0 apple
2 banana
4 leeche
If need check all columns without first:
df = df.loc[df.iloc[:, 1:].ne('False').all(1), ['Id']]
Explanation:
First select columns by columns names:
#if strings
cols = ['0','1','2','3','4','5']
#if numeric
#cols = np.arange(6)
print (df[cols])
0 1 2 3 4 5
0 True None None None None None
1 False None True None None None
2 True None None True None None
3 False None None None True None
4 None True None None None None
Then check if not equal False
by DataFrame.ne
:
#if boolean False
print(df[cols].ne(False))
#if string False
#print(df[cols].ne('False'))
0 1 2 3 4 5
0 True True True True True True
1 False True True True True True
2 True True True True True True
3 False True True True True True
4 True True True True True True
And test if all True
s per rows by DataFrame.all
:
print(df[cols].ne('False').all(1))
0 True
1 False
2 True
3 False
4 True
dtype: bool
Last filtering by boolean indexing
with select Id
with []
for one column DataFrame
:
print(df[df[cols].ne('False').all(1)])
Id 0 1 2 3 4 5
0 apple True None None None None None
2 banana True None None True None None
4 leeche None True None None None None
print(df.loc[df[cols].ne('False').all(1), ['Id']])
Id
0 apple
2 banana
4 leeche
Upvotes: 3