You can use all with boolean indexing:
print ((df == 'something1').all(1))
0 True
1 False
2 True
3 False
4 False
dtype: bool
print (df[(df == 'something1').all(1)])
col1 col2
0 something1 something1
2 something1 something1
EDIT:
If need select only some columns you can use isin with boolean indexing for selecting desired columns and then use subset - df[cols]:
print (df)
col1 col2 col3
0 something1 something1 a
1 something2 something3 s
2 something1 something1 r
3 something2 something3 a
4 something1 something2 a
cols = df.columns[df.columns.isin(['col1','col2'])]
print (cols)
Index(['col1', 'col2'], dtype='object')
print (df[(df[cols] == 'something1').all(1)])
col1 col2 col3
0 something1 something1 a
2 something1 something1 r
Answer from jezrael on Stack OverflowYou can use all with boolean indexing:
print ((df == 'something1').all(1))
0 True
1 False
2 True
3 False
4 False
dtype: bool
print (df[(df == 'something1').all(1)])
col1 col2
0 something1 something1
2 something1 something1
EDIT:
If need select only some columns you can use isin with boolean indexing for selecting desired columns and then use subset - df[cols]:
print (df)
col1 col2 col3
0 something1 something1 a
1 something2 something3 s
2 something1 something1 r
3 something2 something3 a
4 something1 something2 a
cols = df.columns[df.columns.isin(['col1','col2'])]
print (cols)
Index(['col1', 'col2'], dtype='object')
print (df[(df[cols] == 'something1').all(1)])
col1 col2 col3
0 something1 something1 a
2 something1 something1 r
Why not:
df[(df.col1 == 'something1') | (df.col2 == 'something1')]
outputs:
col1 col2
0 something1 something1
2 something1 something1
4 something1 something2
Selecting Columns Based on Condition
Pandas Python, select columns based on rows conditions - Stack Overflow
Filtering a pandas float column by “less than”
How do I count the number of values greater than > 3 for every column in a Pandas DataFrame?
Hey, everyone! This should be super easy and I think I'm having Monday-brain, but I'm having trouble trying to trying to do something to rows based on particular conditions. Here's some pseudo data:
d = {'Col 1': ['a', 'b'], 'Col 1 %': [1, 2], 'Col 2': ['c', 'd'], 'Col 2 %': [3, 4], 'Col 3': ['e', 'f']'Col 3 %': [2, 4]}
fake_df = pd.DataFrame(data=d)
fake_dfThe df looks like...
Col 1 Col 1 % Col 2 Col 2 % Col 3 Col 3 % 0 a 1 c 3 e 2 1 b 2 d 4 f 4
Now let's say I want to do a sum across all rows. Now it won't work by doing sum because not all of these are ints/floats. What I'm currently doing to do this is:
fake_df['Total'] = fake_df.columns[:-1] == "%".sum()
The error is:
AttributeError: 'str' object has no attribute 'sum'
Now I get it. I know it's not working, but Google isn't really too kind because when I search for the answer, it's thinking I'm searching based on some of the values within the data, not the columns. My intended solution is something like:
Total 0 6 1 10
Now for the hell of it, I tried:
fake_df['Total'] = fake_df.sum()
Which, as expected, resulted in:
Col 1 Col 1 % Col 2 Col 2 % Col 3 Col 3 % Total 0 a 1 c 3 e 2 NaN 1 b 2 d 4 f 4 NaN
I'm going to ask that the response not use any argument within the sum function to skip that because in my actual df problem, I only want to use rows that end in "%", and not all numeric-only columns end in that.
Thanks!
Use gt and any to filter the df:
In [287]:
df.ix[:,df.gt(2).any()]
Out[287]:
2
0 1.590124
1 2.500397
Here we use ix to select all rows, the first : and the next arg is a boolean mask of the columns that meet the condition:
In [288]:
df.gt(2)
Out[288]:
0 1 2 3
0 False False False False
1 False False True False
In [289]:
df.gt(2).any()
Out[289]:
0 False
1 False
2 True
3 False
dtype: bool
In your example what you did was select the cell value for the first row and second column, you then tried to use this to mask the columns but this just returned the first column hence why it didn't work:
In [291]:
df.iloc[(0,1)]
Out[291]:
1.3296030000000001
In [293]:
df.columns[df.iloc[(0,1)]>2]
Out[293]:
'0'
Use mask created with df > 2 with any and then select columns by ix:
import pandas as pd
np.random.seed(18)
df = pd.DataFrame(np.random.randn(2, 4))
print(df)
0 1 2 3
0 0.079428 2.190202 -0.134892 0.160518
1 0.442698 0.623391 1.008903 0.394249
print ((df>2).any())
0 False
1 True
2 False
3 False
dtype: bool
print (df.ix[:, (df>2).any()])
1
0 2.190202
1 0.623391
EDIT by comment:
You can check your solution per partes:
It seems it works, but it always select second column (1, python count from 0) column if condition True:
print (df.iloc[(0,1)])
2.19020235741
print (df.iloc[(0,1)] > 2)
True
print (df.columns[df.iloc[(0,1)]>2])
1
print (df[df.columns[df.iloc[(0,1)]>2]])
0 2.190202
1 0.623391
Name: 1, dtype: float64
And first column (0) column if False, because boolean True and False are casted to 1 and 0:
np.random.seed(15)
df = pd.DataFrame(np.random.randn(2, 4))
print (df)
0 1 2 3
0 -0.312328 0.339285 -0.155909 -0.501790
1 0.235569 -1.763605 -1.095862 -1.087766
print (df.iloc[(0,1)])
0.339284706046
print (df.iloc[(0,1)] > 2)
False
print (df.columns[df.iloc[(0,1)]>2])
0
print (df[df.columns[df.iloc[(0,1)]>2]])
0 -0.312328
1 0.235569
Name: 0, dtype: float64
If change column names:
np.random.seed(15)
df = pd.DataFrame(np.random.randn(2, 4))
df.columns = ['a','b','c','d']
print (df)
a b c d
0 -0.312328 0.339285 -0.155909 -0.501790
1 0.235569 -1.763605 -1.095862 -1.087766
print (df.iloc[(0,1)] > 2)
False
print (df[df.columns[df.iloc[(0,1)]>2]])
0 -0.312328
1 0.235569
Name: a, dtype: float64