I need to compare columns within a same dataframe and rank them
I have a dataframe with 6 columns I need to compare each 3 columns with other three columns another. The 6 columns are same data but values of first 3 are from one method and other three are other method. So I need to compare them for differences or variations.
Df.head()
A B C A-1 B-1 C-1
190 289 300 190 287 267
And my conditions are,
conditions = [(combined_min['A'] == combined_min['A-1']) & (combined_min['B'] == combined_min['B-1'] & combined_min['C'] == combined_min['C-1']),
(combined_min['A'] > combined_min['A-1']) & (combined_min['B'] > combined_min['B-1'] & combined_min['C'] > combined_min['C-1']),
(combined_min['A'] < combined_min['A-1']) & (combined_min['B'] < combined_min['B-1'] & combined_min['C'] < combined_min['C-1'])]
And my choices are,
choices = [ "same", 'kj_greater', 'mi_greater' ]
Then I tried,
combined_min['que'] = np.select(conditions,choices, default=np.nan)
But it is throwing error message,
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
In the end I need a dataframe like this,
A B C A-1 B-1 C-1 que
190 289 300 190 287 267 kj_greater
The if the columns A, B, and C, are higher then kj_greater otherwise mi_greater, if all 6 are same then same.
python pandas numpy
add a comment |
I have a dataframe with 6 columns I need to compare each 3 columns with other three columns another. The 6 columns are same data but values of first 3 are from one method and other three are other method. So I need to compare them for differences or variations.
Df.head()
A B C A-1 B-1 C-1
190 289 300 190 287 267
And my conditions are,
conditions = [(combined_min['A'] == combined_min['A-1']) & (combined_min['B'] == combined_min['B-1'] & combined_min['C'] == combined_min['C-1']),
(combined_min['A'] > combined_min['A-1']) & (combined_min['B'] > combined_min['B-1'] & combined_min['C'] > combined_min['C-1']),
(combined_min['A'] < combined_min['A-1']) & (combined_min['B'] < combined_min['B-1'] & combined_min['C'] < combined_min['C-1'])]
And my choices are,
choices = [ "same", 'kj_greater', 'mi_greater' ]
Then I tried,
combined_min['que'] = np.select(conditions,choices, default=np.nan)
But it is throwing error message,
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
In the end I need a dataframe like this,
A B C A-1 B-1 C-1 que
190 289 300 190 287 267 kj_greater
The if the columns A, B, and C, are higher then kj_greater otherwise mi_greater, if all 6 are same then same.
python pandas numpy
this is due to operator precedence. You should enclose each condition under a parenthesis .
– anky_91
Nov 20 '18 at 11:24
With your above data, I think the result will becombined_min['que'] == np.nan
, sincecombined_min['A'] == combined_min['A-1'] == 190
. Maybe makecombined_min['A'] == 191
in your example?
– tel
Nov 20 '18 at 13:13
add a comment |
I have a dataframe with 6 columns I need to compare each 3 columns with other three columns another. The 6 columns are same data but values of first 3 are from one method and other three are other method. So I need to compare them for differences or variations.
Df.head()
A B C A-1 B-1 C-1
190 289 300 190 287 267
And my conditions are,
conditions = [(combined_min['A'] == combined_min['A-1']) & (combined_min['B'] == combined_min['B-1'] & combined_min['C'] == combined_min['C-1']),
(combined_min['A'] > combined_min['A-1']) & (combined_min['B'] > combined_min['B-1'] & combined_min['C'] > combined_min['C-1']),
(combined_min['A'] < combined_min['A-1']) & (combined_min['B'] < combined_min['B-1'] & combined_min['C'] < combined_min['C-1'])]
And my choices are,
choices = [ "same", 'kj_greater', 'mi_greater' ]
Then I tried,
combined_min['que'] = np.select(conditions,choices, default=np.nan)
But it is throwing error message,
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
In the end I need a dataframe like this,
A B C A-1 B-1 C-1 que
190 289 300 190 287 267 kj_greater
The if the columns A, B, and C, are higher then kj_greater otherwise mi_greater, if all 6 are same then same.
python pandas numpy
I have a dataframe with 6 columns I need to compare each 3 columns with other three columns another. The 6 columns are same data but values of first 3 are from one method and other three are other method. So I need to compare them for differences or variations.
Df.head()
A B C A-1 B-1 C-1
190 289 300 190 287 267
And my conditions are,
conditions = [(combined_min['A'] == combined_min['A-1']) & (combined_min['B'] == combined_min['B-1'] & combined_min['C'] == combined_min['C-1']),
(combined_min['A'] > combined_min['A-1']) & (combined_min['B'] > combined_min['B-1'] & combined_min['C'] > combined_min['C-1']),
(combined_min['A'] < combined_min['A-1']) & (combined_min['B'] < combined_min['B-1'] & combined_min['C'] < combined_min['C-1'])]
And my choices are,
choices = [ "same", 'kj_greater', 'mi_greater' ]
Then I tried,
combined_min['que'] = np.select(conditions,choices, default=np.nan)
But it is throwing error message,
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
In the end I need a dataframe like this,
A B C A-1 B-1 C-1 que
190 289 300 190 287 267 kj_greater
The if the columns A, B, and C, are higher then kj_greater otherwise mi_greater, if all 6 are same then same.
python pandas numpy
python pandas numpy
edited Nov 20 '18 at 11:58
tel
7,41621431
7,41621431
asked Nov 20 '18 at 10:51
user1017373user1017373
71611025
71611025
this is due to operator precedence. You should enclose each condition under a parenthesis .
– anky_91
Nov 20 '18 at 11:24
With your above data, I think the result will becombined_min['que'] == np.nan
, sincecombined_min['A'] == combined_min['A-1'] == 190
. Maybe makecombined_min['A'] == 191
in your example?
– tel
Nov 20 '18 at 13:13
add a comment |
this is due to operator precedence. You should enclose each condition under a parenthesis .
– anky_91
Nov 20 '18 at 11:24
With your above data, I think the result will becombined_min['que'] == np.nan
, sincecombined_min['A'] == combined_min['A-1'] == 190
. Maybe makecombined_min['A'] == 191
in your example?
– tel
Nov 20 '18 at 13:13
this is due to operator precedence. You should enclose each condition under a parenthesis .
– anky_91
Nov 20 '18 at 11:24
this is due to operator precedence. You should enclose each condition under a parenthesis .
– anky_91
Nov 20 '18 at 11:24
With your above data, I think the result will be
combined_min['que'] == np.nan
, since combined_min['A'] == combined_min['A-1'] == 190
. Maybe make combined_min['A'] == 191
in your example?– tel
Nov 20 '18 at 13:13
With your above data, I think the result will be
combined_min['que'] == np.nan
, since combined_min['A'] == combined_min['A-1'] == 190
. Maybe make combined_min['A'] == 191
in your example?– tel
Nov 20 '18 at 13:13
add a comment |
3 Answers
3
active
oldest
votes
Edit
After a bit of digging/reflection, I realized that I was wrong: it turns out that &
is a logical operator in Pandas. &
implements pairwise logical and between pd.Series
and pd.DataFrame
objects. Unfortunately, &
has different operator precedence than and
, so you have to be careful with it (in this case, &
has higher precedence than ==
, >
, or <
). The bug in the OP's code just comes down to a lack of parentheses in the right places.
So to get the kind of labeling that the OP was originally after, the code would be:
import numpy as np
import pandas as pd
data= [
[191, 289, 300, 190, 287, 267],
[191, 289, 300, 200, 312, 400],
[191, 289, 300, 191, 289, 300],
[191, 289, 300, 200, 287, 400],
]
combined_min = pd.DataFrame(data=data, columns=['A', 'B','C','A-1','B-1','C-1'])
cond = lambda x: [(x['A'] == x['A-1']) & (x['B'] == x['B-1']) & (x['C'] == x['C-1']),
(x['A'] > x['A-1']) & (x['B'] > x['B-1']) & (x['C'] > x['C-1']),
(x['A'] < x['A-1']) & (x['B'] < x['B-1']) & (x['C'] < x['C-1'])]
choices = ['same', 'kj_greater', 'mi_greater']
combined_min['que'] = np.select(cond(combined_min), choices, default=np.nan)
print(combined_min)
This outputs:
A B C A-1 B-1 C-1 que
0 191 289 300 190 287 267 kj_greater
1 191 289 300 200 312 400 mi_greater
2 191 289 300 191 289 300 same
3 191 289 300 200 287 400 nan
Optionally, cond
can be boiled down to a one-liner:
from functools import reduce
from operator import eq, gt, lt, and_
cond = lambda x: [reduce(and_, (op(x[c], x['{}-1'.format(c)]) for c in 'ABC')) for op in (eq, gt, lt)]
Though this reduces readability somewhat.
Thanks , it is printing NAH for all rows
– user1017373
Nov 20 '18 at 14:57
@user1017373 I realized that I was wrong about&
not being a logical operator, at least for Pandas objects. I posted a corrected answer with code that should label each row according to your original intention.
– tel
Nov 20 '18 at 16:55
Thanks with this new editing it complains TypeError: object of type 'function' has no len()
– user1017373
Nov 21 '18 at 13:12
@user1017373 I think I know what might be causing thatTypeError
. Is there a line in your code that looks like:np.select(cond, choices, default=np.nan)
? You can't pass thecond
lambda directly tonp.select
, you have to call it and pass the result. The line should instead look likenp.select(cond(combined_min), choices, default=np.nan)
. If you're having trouble, first try just copy/pasting the code from my answer and see if you can get that to run as is.
– tel
Nov 22 '18 at 1:13
add a comment |
The problem is that you are missing parenthesis on conditions. Each conditions has to be surrounded by parenthesis.
conditions = [(combined_min['A'] == combined_min['A-1']) & (combined_min['B'] == combined_min['B-1']) & (combined_min['C'] == combined_min['C-1']),
(combined_min['A'] > combined_min['A-1']) & (combined_min['B'] > combined_min['B-1']) & (combined_min['C'] > combined_min['C-1']),
(combined_min['A'] < combined_min['A-1']) & (combined_min['B'] < combined_min['B-1']) & (combined_min['C'] < combined_min['C-1'])]
add a comment |
You're error is in your conditions. The problem is that you are not directly comparing booleans, but rather a set of pd.Series containing a boolean, which connot be directly compared as you do.
So:
df['A'] == df['A-1']
Returns:
0 True
dtype: bool
So when you do:
df['A'] == df['A-1'] & df['A'] == df['A-1']
You get the error you mentioned. Try separating each term using parenthesis, and using any()
to get the boolean from the pd.Series:
((df['A'] == df['A-1']) & (df['A'] == df['A-1'])).any()
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391390%2fi-need-to-compare-columns-within-a-same-dataframe-and-rank-them%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Edit
After a bit of digging/reflection, I realized that I was wrong: it turns out that &
is a logical operator in Pandas. &
implements pairwise logical and between pd.Series
and pd.DataFrame
objects. Unfortunately, &
has different operator precedence than and
, so you have to be careful with it (in this case, &
has higher precedence than ==
, >
, or <
). The bug in the OP's code just comes down to a lack of parentheses in the right places.
So to get the kind of labeling that the OP was originally after, the code would be:
import numpy as np
import pandas as pd
data= [
[191, 289, 300, 190, 287, 267],
[191, 289, 300, 200, 312, 400],
[191, 289, 300, 191, 289, 300],
[191, 289, 300, 200, 287, 400],
]
combined_min = pd.DataFrame(data=data, columns=['A', 'B','C','A-1','B-1','C-1'])
cond = lambda x: [(x['A'] == x['A-1']) & (x['B'] == x['B-1']) & (x['C'] == x['C-1']),
(x['A'] > x['A-1']) & (x['B'] > x['B-1']) & (x['C'] > x['C-1']),
(x['A'] < x['A-1']) & (x['B'] < x['B-1']) & (x['C'] < x['C-1'])]
choices = ['same', 'kj_greater', 'mi_greater']
combined_min['que'] = np.select(cond(combined_min), choices, default=np.nan)
print(combined_min)
This outputs:
A B C A-1 B-1 C-1 que
0 191 289 300 190 287 267 kj_greater
1 191 289 300 200 312 400 mi_greater
2 191 289 300 191 289 300 same
3 191 289 300 200 287 400 nan
Optionally, cond
can be boiled down to a one-liner:
from functools import reduce
from operator import eq, gt, lt, and_
cond = lambda x: [reduce(and_, (op(x[c], x['{}-1'.format(c)]) for c in 'ABC')) for op in (eq, gt, lt)]
Though this reduces readability somewhat.
Thanks , it is printing NAH for all rows
– user1017373
Nov 20 '18 at 14:57
@user1017373 I realized that I was wrong about&
not being a logical operator, at least for Pandas objects. I posted a corrected answer with code that should label each row according to your original intention.
– tel
Nov 20 '18 at 16:55
Thanks with this new editing it complains TypeError: object of type 'function' has no len()
– user1017373
Nov 21 '18 at 13:12
@user1017373 I think I know what might be causing thatTypeError
. Is there a line in your code that looks like:np.select(cond, choices, default=np.nan)
? You can't pass thecond
lambda directly tonp.select
, you have to call it and pass the result. The line should instead look likenp.select(cond(combined_min), choices, default=np.nan)
. If you're having trouble, first try just copy/pasting the code from my answer and see if you can get that to run as is.
– tel
Nov 22 '18 at 1:13
add a comment |
Edit
After a bit of digging/reflection, I realized that I was wrong: it turns out that &
is a logical operator in Pandas. &
implements pairwise logical and between pd.Series
and pd.DataFrame
objects. Unfortunately, &
has different operator precedence than and
, so you have to be careful with it (in this case, &
has higher precedence than ==
, >
, or <
). The bug in the OP's code just comes down to a lack of parentheses in the right places.
So to get the kind of labeling that the OP was originally after, the code would be:
import numpy as np
import pandas as pd
data= [
[191, 289, 300, 190, 287, 267],
[191, 289, 300, 200, 312, 400],
[191, 289, 300, 191, 289, 300],
[191, 289, 300, 200, 287, 400],
]
combined_min = pd.DataFrame(data=data, columns=['A', 'B','C','A-1','B-1','C-1'])
cond = lambda x: [(x['A'] == x['A-1']) & (x['B'] == x['B-1']) & (x['C'] == x['C-1']),
(x['A'] > x['A-1']) & (x['B'] > x['B-1']) & (x['C'] > x['C-1']),
(x['A'] < x['A-1']) & (x['B'] < x['B-1']) & (x['C'] < x['C-1'])]
choices = ['same', 'kj_greater', 'mi_greater']
combined_min['que'] = np.select(cond(combined_min), choices, default=np.nan)
print(combined_min)
This outputs:
A B C A-1 B-1 C-1 que
0 191 289 300 190 287 267 kj_greater
1 191 289 300 200 312 400 mi_greater
2 191 289 300 191 289 300 same
3 191 289 300 200 287 400 nan
Optionally, cond
can be boiled down to a one-liner:
from functools import reduce
from operator import eq, gt, lt, and_
cond = lambda x: [reduce(and_, (op(x[c], x['{}-1'.format(c)]) for c in 'ABC')) for op in (eq, gt, lt)]
Though this reduces readability somewhat.
Thanks , it is printing NAH for all rows
– user1017373
Nov 20 '18 at 14:57
@user1017373 I realized that I was wrong about&
not being a logical operator, at least for Pandas objects. I posted a corrected answer with code that should label each row according to your original intention.
– tel
Nov 20 '18 at 16:55
Thanks with this new editing it complains TypeError: object of type 'function' has no len()
– user1017373
Nov 21 '18 at 13:12
@user1017373 I think I know what might be causing thatTypeError
. Is there a line in your code that looks like:np.select(cond, choices, default=np.nan)
? You can't pass thecond
lambda directly tonp.select
, you have to call it and pass the result. The line should instead look likenp.select(cond(combined_min), choices, default=np.nan)
. If you're having trouble, first try just copy/pasting the code from my answer and see if you can get that to run as is.
– tel
Nov 22 '18 at 1:13
add a comment |
Edit
After a bit of digging/reflection, I realized that I was wrong: it turns out that &
is a logical operator in Pandas. &
implements pairwise logical and between pd.Series
and pd.DataFrame
objects. Unfortunately, &
has different operator precedence than and
, so you have to be careful with it (in this case, &
has higher precedence than ==
, >
, or <
). The bug in the OP's code just comes down to a lack of parentheses in the right places.
So to get the kind of labeling that the OP was originally after, the code would be:
import numpy as np
import pandas as pd
data= [
[191, 289, 300, 190, 287, 267],
[191, 289, 300, 200, 312, 400],
[191, 289, 300, 191, 289, 300],
[191, 289, 300, 200, 287, 400],
]
combined_min = pd.DataFrame(data=data, columns=['A', 'B','C','A-1','B-1','C-1'])
cond = lambda x: [(x['A'] == x['A-1']) & (x['B'] == x['B-1']) & (x['C'] == x['C-1']),
(x['A'] > x['A-1']) & (x['B'] > x['B-1']) & (x['C'] > x['C-1']),
(x['A'] < x['A-1']) & (x['B'] < x['B-1']) & (x['C'] < x['C-1'])]
choices = ['same', 'kj_greater', 'mi_greater']
combined_min['que'] = np.select(cond(combined_min), choices, default=np.nan)
print(combined_min)
This outputs:
A B C A-1 B-1 C-1 que
0 191 289 300 190 287 267 kj_greater
1 191 289 300 200 312 400 mi_greater
2 191 289 300 191 289 300 same
3 191 289 300 200 287 400 nan
Optionally, cond
can be boiled down to a one-liner:
from functools import reduce
from operator import eq, gt, lt, and_
cond = lambda x: [reduce(and_, (op(x[c], x['{}-1'.format(c)]) for c in 'ABC')) for op in (eq, gt, lt)]
Though this reduces readability somewhat.
Edit
After a bit of digging/reflection, I realized that I was wrong: it turns out that &
is a logical operator in Pandas. &
implements pairwise logical and between pd.Series
and pd.DataFrame
objects. Unfortunately, &
has different operator precedence than and
, so you have to be careful with it (in this case, &
has higher precedence than ==
, >
, or <
). The bug in the OP's code just comes down to a lack of parentheses in the right places.
So to get the kind of labeling that the OP was originally after, the code would be:
import numpy as np
import pandas as pd
data= [
[191, 289, 300, 190, 287, 267],
[191, 289, 300, 200, 312, 400],
[191, 289, 300, 191, 289, 300],
[191, 289, 300, 200, 287, 400],
]
combined_min = pd.DataFrame(data=data, columns=['A', 'B','C','A-1','B-1','C-1'])
cond = lambda x: [(x['A'] == x['A-1']) & (x['B'] == x['B-1']) & (x['C'] == x['C-1']),
(x['A'] > x['A-1']) & (x['B'] > x['B-1']) & (x['C'] > x['C-1']),
(x['A'] < x['A-1']) & (x['B'] < x['B-1']) & (x['C'] < x['C-1'])]
choices = ['same', 'kj_greater', 'mi_greater']
combined_min['que'] = np.select(cond(combined_min), choices, default=np.nan)
print(combined_min)
This outputs:
A B C A-1 B-1 C-1 que
0 191 289 300 190 287 267 kj_greater
1 191 289 300 200 312 400 mi_greater
2 191 289 300 191 289 300 same
3 191 289 300 200 287 400 nan
Optionally, cond
can be boiled down to a one-liner:
from functools import reduce
from operator import eq, gt, lt, and_
cond = lambda x: [reduce(and_, (op(x[c], x['{}-1'.format(c)]) for c in 'ABC')) for op in (eq, gt, lt)]
Though this reduces readability somewhat.
edited Nov 20 '18 at 16:53
answered Nov 20 '18 at 11:22
teltel
7,41621431
7,41621431
Thanks , it is printing NAH for all rows
– user1017373
Nov 20 '18 at 14:57
@user1017373 I realized that I was wrong about&
not being a logical operator, at least for Pandas objects. I posted a corrected answer with code that should label each row according to your original intention.
– tel
Nov 20 '18 at 16:55
Thanks with this new editing it complains TypeError: object of type 'function' has no len()
– user1017373
Nov 21 '18 at 13:12
@user1017373 I think I know what might be causing thatTypeError
. Is there a line in your code that looks like:np.select(cond, choices, default=np.nan)
? You can't pass thecond
lambda directly tonp.select
, you have to call it and pass the result. The line should instead look likenp.select(cond(combined_min), choices, default=np.nan)
. If you're having trouble, first try just copy/pasting the code from my answer and see if you can get that to run as is.
– tel
Nov 22 '18 at 1:13
add a comment |
Thanks , it is printing NAH for all rows
– user1017373
Nov 20 '18 at 14:57
@user1017373 I realized that I was wrong about&
not being a logical operator, at least for Pandas objects. I posted a corrected answer with code that should label each row according to your original intention.
– tel
Nov 20 '18 at 16:55
Thanks with this new editing it complains TypeError: object of type 'function' has no len()
– user1017373
Nov 21 '18 at 13:12
@user1017373 I think I know what might be causing thatTypeError
. Is there a line in your code that looks like:np.select(cond, choices, default=np.nan)
? You can't pass thecond
lambda directly tonp.select
, you have to call it and pass the result. The line should instead look likenp.select(cond(combined_min), choices, default=np.nan)
. If you're having trouble, first try just copy/pasting the code from my answer and see if you can get that to run as is.
– tel
Nov 22 '18 at 1:13
Thanks , it is printing NAH for all rows
– user1017373
Nov 20 '18 at 14:57
Thanks , it is printing NAH for all rows
– user1017373
Nov 20 '18 at 14:57
@user1017373 I realized that I was wrong about
&
not being a logical operator, at least for Pandas objects. I posted a corrected answer with code that should label each row according to your original intention.– tel
Nov 20 '18 at 16:55
@user1017373 I realized that I was wrong about
&
not being a logical operator, at least for Pandas objects. I posted a corrected answer with code that should label each row according to your original intention.– tel
Nov 20 '18 at 16:55
Thanks with this new editing it complains TypeError: object of type 'function' has no len()
– user1017373
Nov 21 '18 at 13:12
Thanks with this new editing it complains TypeError: object of type 'function' has no len()
– user1017373
Nov 21 '18 at 13:12
@user1017373 I think I know what might be causing that
TypeError
. Is there a line in your code that looks like: np.select(cond, choices, default=np.nan)
? You can't pass the cond
lambda directly to np.select
, you have to call it and pass the result. The line should instead look like np.select(cond(combined_min), choices, default=np.nan)
. If you're having trouble, first try just copy/pasting the code from my answer and see if you can get that to run as is.– tel
Nov 22 '18 at 1:13
@user1017373 I think I know what might be causing that
TypeError
. Is there a line in your code that looks like: np.select(cond, choices, default=np.nan)
? You can't pass the cond
lambda directly to np.select
, you have to call it and pass the result. The line should instead look like np.select(cond(combined_min), choices, default=np.nan)
. If you're having trouble, first try just copy/pasting the code from my answer and see if you can get that to run as is.– tel
Nov 22 '18 at 1:13
add a comment |
The problem is that you are missing parenthesis on conditions. Each conditions has to be surrounded by parenthesis.
conditions = [(combined_min['A'] == combined_min['A-1']) & (combined_min['B'] == combined_min['B-1']) & (combined_min['C'] == combined_min['C-1']),
(combined_min['A'] > combined_min['A-1']) & (combined_min['B'] > combined_min['B-1']) & (combined_min['C'] > combined_min['C-1']),
(combined_min['A'] < combined_min['A-1']) & (combined_min['B'] < combined_min['B-1']) & (combined_min['C'] < combined_min['C-1'])]
add a comment |
The problem is that you are missing parenthesis on conditions. Each conditions has to be surrounded by parenthesis.
conditions = [(combined_min['A'] == combined_min['A-1']) & (combined_min['B'] == combined_min['B-1']) & (combined_min['C'] == combined_min['C-1']),
(combined_min['A'] > combined_min['A-1']) & (combined_min['B'] > combined_min['B-1']) & (combined_min['C'] > combined_min['C-1']),
(combined_min['A'] < combined_min['A-1']) & (combined_min['B'] < combined_min['B-1']) & (combined_min['C'] < combined_min['C-1'])]
add a comment |
The problem is that you are missing parenthesis on conditions. Each conditions has to be surrounded by parenthesis.
conditions = [(combined_min['A'] == combined_min['A-1']) & (combined_min['B'] == combined_min['B-1']) & (combined_min['C'] == combined_min['C-1']),
(combined_min['A'] > combined_min['A-1']) & (combined_min['B'] > combined_min['B-1']) & (combined_min['C'] > combined_min['C-1']),
(combined_min['A'] < combined_min['A-1']) & (combined_min['B'] < combined_min['B-1']) & (combined_min['C'] < combined_min['C-1'])]
The problem is that you are missing parenthesis on conditions. Each conditions has to be surrounded by parenthesis.
conditions = [(combined_min['A'] == combined_min['A-1']) & (combined_min['B'] == combined_min['B-1']) & (combined_min['C'] == combined_min['C-1']),
(combined_min['A'] > combined_min['A-1']) & (combined_min['B'] > combined_min['B-1']) & (combined_min['C'] > combined_min['C-1']),
(combined_min['A'] < combined_min['A-1']) & (combined_min['B'] < combined_min['B-1']) & (combined_min['C'] < combined_min['C-1'])]
answered Nov 20 '18 at 11:24
TzomasTzomas
507314
507314
add a comment |
add a comment |
You're error is in your conditions. The problem is that you are not directly comparing booleans, but rather a set of pd.Series containing a boolean, which connot be directly compared as you do.
So:
df['A'] == df['A-1']
Returns:
0 True
dtype: bool
So when you do:
df['A'] == df['A-1'] & df['A'] == df['A-1']
You get the error you mentioned. Try separating each term using parenthesis, and using any()
to get the boolean from the pd.Series:
((df['A'] == df['A-1']) & (df['A'] == df['A-1'])).any()
add a comment |
You're error is in your conditions. The problem is that you are not directly comparing booleans, but rather a set of pd.Series containing a boolean, which connot be directly compared as you do.
So:
df['A'] == df['A-1']
Returns:
0 True
dtype: bool
So when you do:
df['A'] == df['A-1'] & df['A'] == df['A-1']
You get the error you mentioned. Try separating each term using parenthesis, and using any()
to get the boolean from the pd.Series:
((df['A'] == df['A-1']) & (df['A'] == df['A-1'])).any()
add a comment |
You're error is in your conditions. The problem is that you are not directly comparing booleans, but rather a set of pd.Series containing a boolean, which connot be directly compared as you do.
So:
df['A'] == df['A-1']
Returns:
0 True
dtype: bool
So when you do:
df['A'] == df['A-1'] & df['A'] == df['A-1']
You get the error you mentioned. Try separating each term using parenthesis, and using any()
to get the boolean from the pd.Series:
((df['A'] == df['A-1']) & (df['A'] == df['A-1'])).any()
You're error is in your conditions. The problem is that you are not directly comparing booleans, but rather a set of pd.Series containing a boolean, which connot be directly compared as you do.
So:
df['A'] == df['A-1']
Returns:
0 True
dtype: bool
So when you do:
df['A'] == df['A-1'] & df['A'] == df['A-1']
You get the error you mentioned. Try separating each term using parenthesis, and using any()
to get the boolean from the pd.Series:
((df['A'] == df['A-1']) & (df['A'] == df['A-1'])).any()
edited Nov 20 '18 at 11:37
answered Nov 20 '18 at 11:23
yatuyatu
11k31036
11k31036
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391390%2fi-need-to-compare-columns-within-a-same-dataframe-and-rank-them%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
this is due to operator precedence. You should enclose each condition under a parenthesis .
– anky_91
Nov 20 '18 at 11:24
With your above data, I think the result will be
combined_min['que'] == np.nan
, sincecombined_min['A'] == combined_min['A-1'] == 190
. Maybe makecombined_min['A'] == 191
in your example?– tel
Nov 20 '18 at 13:13