Reputation: 918
I have a dataframe which I need to filter if the value of two columns for each row is bigger than two specific numbers. Also, it's important that I don't know anything of the dataframe except that those columns are the numbers -2 and -3
For example, we have dataframe = pd.DataFrame(data=[[1,2,3,4],[5,6,7,8],[1,2,-3,4],[5,-6,7,8]])
. Imagine that we need to filter it if the -2 and -3 columns are bigger than 0. In this case, we'll return only the first two rows.
I've tried it using return dataframe[(dataframe[:, -3] >= 0) and (dataframe[:, -2] >= 0)]
, but it gives me the following error: TypeError: unhashable type: 'slice'
.
Upvotes: 2
Views: 3517
Reputation: 863116
Use iloc
for select columns by positions and for botwise and
use &
:
df = dataframe[(dataframe.iloc[:, -3] >= 0) & (dataframe.iloc[:, -2] >= 0)]
print (df)
0 1 2 3
0 1 2 3 4
1 5 6 7 8
Upvotes: 5
Reputation: 61910
You could do:
import pandas as pd
dataframe = pd.DataFrame(data=[[1,2,3,4],
[5,6,7,8],
[1,2,-3,4],
[5,-6,7,8]])
mask = (dataframe.iloc[:, [-3, 2]] >= 0).all(axis=1)
result = dataframe[mask]
print(result)
Output
0 1 2 3
0 1 2 3 4
1 5 6 7 8
Upvotes: 1