Python Pandas: Boolean indexing on multiple columns [duplicate]selecting across multiple columns with python pandas?Filtering Panda DataFrameReplace row in DataFrame dependant on a valueFill NaN with a known valueChange entire pandas Series based on conditionsCalling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonDoes Python have a ternary conditional operator?Accessing the index in 'for' loops?Does Python have a string 'contains' substring method?Selecting multiple columns in a pandas dataframeRenaming columns in pandasDelete column from pandas DataFrame by column nameSelect rows from a DataFrame based on values in a column in pandas
Font hinting is lost in Chrome-like browsers (for some languages )
tikz: show 0 at the axis origin
The use of multiple foreign keys on same column in SQL Server
Why was the small council so happy for Tyrion to become the Master of Coin?
Why "Having chlorophyll without photosynthesis is actually very dangerous" and "like living with a bomb"?
How did the USSR manage to innovate in an environment characterized by government censorship and high bureaucracy?
What do the dots in this tr command do: tr .............A-Z A-ZA-Z <<< "JVPQBOV" (with 13 dots)
Is this a crack on the carbon frame?
How do I create uniquely male characters?
Approximately how much travel time was saved by the opening of the Suez Canal in 1869?
What defenses are there against being summoned by the Gate spell?
How to format long polynomial?
Prove that NP is closed under karp reduction?
What is the word for reserving something for yourself before others do?
What are the differences between the usage of 'it' and 'they'?
strToHex ( string to its hex representation as string)
Can a Warlock become Neutral Good?
How old can references or sources in a thesis be?
Why are 150k or 200k jobs considered good when there are 300k+ births a month?
How do we improve the relationship with a client software team that performs poorly and is becoming less collaborative?
Which models of the Boeing 737 are still in production?
Why, historically, did Gödel think CH was false?
Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?
Writing rule stating superpower from different root cause is bad writing
Python Pandas: Boolean indexing on multiple columns [duplicate]
selecting across multiple columns with python pandas?Filtering Panda DataFrameReplace row in DataFrame dependant on a valueFill NaN with a known valueChange entire pandas Series based on conditionsCalling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonDoes Python have a ternary conditional operator?Accessing the index in 'for' loops?Does Python have a string 'contains' substring method?Selecting multiple columns in a pandas dataframeRenaming columns in pandasDelete column from pandas DataFrame by column nameSelect rows from a DataFrame based on values in a column in pandas
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
This question already has an answer here:
selecting across multiple columns with python pandas?
3 answers
despite there being at least two good tutorials on how to index a DataFrame in Python's pandas
library, I still can't work out an elegant way of SELECT
ing on more than one column.
>>> d = pd.DataFrame('x':[1, 2, 3, 4, 5], 'y':[4, 5, 6, 7, 8])
>>> d
x y
0 1 4
1 2 5
2 3 6
3 4 7
4 5 8
>>> d[d['x']>2] # This works fine
x y
2 3 6
3 4 7
4 5 8
>>> d[d['x']>2 & d['y']>7] # I had expected this to work, but it doesn't
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I have found (what I think is) a rather inelegant way of doing it, like this
>>> d[d['x']>2][d['y']>7]
But it's not pretty, and it scores fairly low for readability (I think).
Is there a better, more Python-tastic way?
python pandas dataframe
marked as duplicate by towi, Pere Villega, skuntsel, Roman C, rael_kid Jun 21 '13 at 11:01
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
selecting across multiple columns with python pandas?
3 answers
despite there being at least two good tutorials on how to index a DataFrame in Python's pandas
library, I still can't work out an elegant way of SELECT
ing on more than one column.
>>> d = pd.DataFrame('x':[1, 2, 3, 4, 5], 'y':[4, 5, 6, 7, 8])
>>> d
x y
0 1 4
1 2 5
2 3 6
3 4 7
4 5 8
>>> d[d['x']>2] # This works fine
x y
2 3 6
3 4 7
4 5 8
>>> d[d['x']>2 & d['y']>7] # I had expected this to work, but it doesn't
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I have found (what I think is) a rather inelegant way of doing it, like this
>>> d[d['x']>2][d['y']>7]
But it's not pretty, and it scores fairly low for readability (I think).
Is there a better, more Python-tastic way?
python pandas dataframe
marked as duplicate by towi, Pere Villega, skuntsel, Roman C, rael_kid Jun 21 '13 at 11:01
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
selecting across multiple columns with python pandas?
3 answers
despite there being at least two good tutorials on how to index a DataFrame in Python's pandas
library, I still can't work out an elegant way of SELECT
ing on more than one column.
>>> d = pd.DataFrame('x':[1, 2, 3, 4, 5], 'y':[4, 5, 6, 7, 8])
>>> d
x y
0 1 4
1 2 5
2 3 6
3 4 7
4 5 8
>>> d[d['x']>2] # This works fine
x y
2 3 6
3 4 7
4 5 8
>>> d[d['x']>2 & d['y']>7] # I had expected this to work, but it doesn't
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I have found (what I think is) a rather inelegant way of doing it, like this
>>> d[d['x']>2][d['y']>7]
But it's not pretty, and it scores fairly low for readability (I think).
Is there a better, more Python-tastic way?
python pandas dataframe
This question already has an answer here:
selecting across multiple columns with python pandas?
3 answers
despite there being at least two good tutorials on how to index a DataFrame in Python's pandas
library, I still can't work out an elegant way of SELECT
ing on more than one column.
>>> d = pd.DataFrame('x':[1, 2, 3, 4, 5], 'y':[4, 5, 6, 7, 8])
>>> d
x y
0 1 4
1 2 5
2 3 6
3 4 7
4 5 8
>>> d[d['x']>2] # This works fine
x y
2 3 6
3 4 7
4 5 8
>>> d[d['x']>2 & d['y']>7] # I had expected this to work, but it doesn't
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I have found (what I think is) a rather inelegant way of doing it, like this
>>> d[d['x']>2][d['y']>7]
But it's not pretty, and it scores fairly low for readability (I think).
Is there a better, more Python-tastic way?
This question already has an answer here:
selecting across multiple columns with python pandas?
3 answers
python pandas dataframe
python pandas dataframe
asked Jun 20 '13 at 14:21
LondonRobLondonRob
28.1k1676117
28.1k1676117
marked as duplicate by towi, Pere Villega, skuntsel, Roman C, rael_kid Jun 21 '13 at 11:01
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by towi, Pere Villega, skuntsel, Roman C, rael_kid Jun 21 '13 at 11:01
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
It is a precedence operator issue.
You should add extra parenthesis to make your multi condition test working:
d[(d['x']>2) & (d['y']>7)]
This section of the tutorial you mentioned shows an example with several boolean conditions and the parenthesis are used.
add a comment |
There may still be a better way, but
In [56]: d[d['x'] > 2] and d[d['y'] > 7]
Out[56]:
x y
4 5 8
works.
1
this works, but ends up using python operators (rather than numpy) and so is going to be much slower
– Jeff
Jun 20 '13 at 14:44
that's a nice solution. I like the fact that it explicitly usesand
. Makes it clearer that there are two conditions being evaluated.
– LondonRob
Jun 20 '13 at 14:45
Oh, I've just found a duplicate of this question. Whoops.
– LondonRob
Jun 20 '13 at 14:48
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
It is a precedence operator issue.
You should add extra parenthesis to make your multi condition test working:
d[(d['x']>2) & (d['y']>7)]
This section of the tutorial you mentioned shows an example with several boolean conditions and the parenthesis are used.
add a comment |
It is a precedence operator issue.
You should add extra parenthesis to make your multi condition test working:
d[(d['x']>2) & (d['y']>7)]
This section of the tutorial you mentioned shows an example with several boolean conditions and the parenthesis are used.
add a comment |
It is a precedence operator issue.
You should add extra parenthesis to make your multi condition test working:
d[(d['x']>2) & (d['y']>7)]
This section of the tutorial you mentioned shows an example with several boolean conditions and the parenthesis are used.
It is a precedence operator issue.
You should add extra parenthesis to make your multi condition test working:
d[(d['x']>2) & (d['y']>7)]
This section of the tutorial you mentioned shows an example with several boolean conditions and the parenthesis are used.
edited Jun 15 '17 at 20:41
A-B-B
24.4k66470
24.4k66470
answered Jun 20 '13 at 14:43
BoudBoud
19.4k64057
19.4k64057
add a comment |
add a comment |
There may still be a better way, but
In [56]: d[d['x'] > 2] and d[d['y'] > 7]
Out[56]:
x y
4 5 8
works.
1
this works, but ends up using python operators (rather than numpy) and so is going to be much slower
– Jeff
Jun 20 '13 at 14:44
that's a nice solution. I like the fact that it explicitly usesand
. Makes it clearer that there are two conditions being evaluated.
– LondonRob
Jun 20 '13 at 14:45
Oh, I've just found a duplicate of this question. Whoops.
– LondonRob
Jun 20 '13 at 14:48
add a comment |
There may still be a better way, but
In [56]: d[d['x'] > 2] and d[d['y'] > 7]
Out[56]:
x y
4 5 8
works.
1
this works, but ends up using python operators (rather than numpy) and so is going to be much slower
– Jeff
Jun 20 '13 at 14:44
that's a nice solution. I like the fact that it explicitly usesand
. Makes it clearer that there are two conditions being evaluated.
– LondonRob
Jun 20 '13 at 14:45
Oh, I've just found a duplicate of this question. Whoops.
– LondonRob
Jun 20 '13 at 14:48
add a comment |
There may still be a better way, but
In [56]: d[d['x'] > 2] and d[d['y'] > 7]
Out[56]:
x y
4 5 8
works.
There may still be a better way, but
In [56]: d[d['x'] > 2] and d[d['y'] > 7]
Out[56]:
x y
4 5 8
works.
answered Jun 20 '13 at 14:33
TomAugspurgerTomAugspurger
16k35456
16k35456
1
this works, but ends up using python operators (rather than numpy) and so is going to be much slower
– Jeff
Jun 20 '13 at 14:44
that's a nice solution. I like the fact that it explicitly usesand
. Makes it clearer that there are two conditions being evaluated.
– LondonRob
Jun 20 '13 at 14:45
Oh, I've just found a duplicate of this question. Whoops.
– LondonRob
Jun 20 '13 at 14:48
add a comment |
1
this works, but ends up using python operators (rather than numpy) and so is going to be much slower
– Jeff
Jun 20 '13 at 14:44
that's a nice solution. I like the fact that it explicitly usesand
. Makes it clearer that there are two conditions being evaluated.
– LondonRob
Jun 20 '13 at 14:45
Oh, I've just found a duplicate of this question. Whoops.
– LondonRob
Jun 20 '13 at 14:48
1
1
this works, but ends up using python operators (rather than numpy) and so is going to be much slower
– Jeff
Jun 20 '13 at 14:44
this works, but ends up using python operators (rather than numpy) and so is going to be much slower
– Jeff
Jun 20 '13 at 14:44
that's a nice solution. I like the fact that it explicitly uses
and
. Makes it clearer that there are two conditions being evaluated.– LondonRob
Jun 20 '13 at 14:45
that's a nice solution. I like the fact that it explicitly uses
and
. Makes it clearer that there are two conditions being evaluated.– LondonRob
Jun 20 '13 at 14:45
Oh, I've just found a duplicate of this question. Whoops.
– LondonRob
Jun 20 '13 at 14:48
Oh, I've just found a duplicate of this question. Whoops.
– LondonRob
Jun 20 '13 at 14:48
add a comment |