Two conditional checks inside a Panda data frame access
The command below works fine
idx = np.asarray(df.loc[df['lat1'] != '.'].ix[:,0].index)
but I'm trying to do something like this (with 2 conditions):
idx = np.asarray(df.loc[df['lat1'] != '.' and df['state'] == df['state'][0]].ix[:,0].index)
This throws the following traceback:
Traceback (most recent call last):
File "<ipython-input-274-c07cda0be195>", line 1, in <module>
idx = np.asarray(df.loc[df['lat1'] != '.' and df['state'] == df['state'][0]].ix[:,0].index)
File "/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py", line 1573, in __nonzero__
.format(self.__class__.__name__))
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I've looked it up but not able to find a suitable tweak for this case. Any leads would be appreciated.
[EDIT]: Based on the suggestion below, when I try this:
df[(df['lat1']!='.') & (df['state']== df['state'][0])]
I get
lat1 long1 ... state county
5 34 11 ... AK Anchorage
7 1 -3 ... AK Anchorage
14 1 -5 ... AK Anchorage
30 7 -3 ... AK Anchorage
44 1 -4 ... AK Anchorage
47 1 -3 ... AK Anchorage
75 1 -4 ... AK Juneau
82 5 -1 ... AK Kenai Peninsula
102 4 -1 ... AK Fairbanks North Star
106 4 -1 ... AK Matanuska Susitna
137 3 -3 ... AK Matanuska Susitna
[11 rows x 5 columns]
How do I extract only the first column containing the indices?
python pandas dataframe
add a comment |
The command below works fine
idx = np.asarray(df.loc[df['lat1'] != '.'].ix[:,0].index)
but I'm trying to do something like this (with 2 conditions):
idx = np.asarray(df.loc[df['lat1'] != '.' and df['state'] == df['state'][0]].ix[:,0].index)
This throws the following traceback:
Traceback (most recent call last):
File "<ipython-input-274-c07cda0be195>", line 1, in <module>
idx = np.asarray(df.loc[df['lat1'] != '.' and df['state'] == df['state'][0]].ix[:,0].index)
File "/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py", line 1573, in __nonzero__
.format(self.__class__.__name__))
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I've looked it up but not able to find a suitable tweak for this case. Any leads would be appreciated.
[EDIT]: Based on the suggestion below, when I try this:
df[(df['lat1']!='.') & (df['state']== df['state'][0])]
I get
lat1 long1 ... state county
5 34 11 ... AK Anchorage
7 1 -3 ... AK Anchorage
14 1 -5 ... AK Anchorage
30 7 -3 ... AK Anchorage
44 1 -4 ... AK Anchorage
47 1 -3 ... AK Anchorage
75 1 -4 ... AK Juneau
82 5 -1 ... AK Kenai Peninsula
102 4 -1 ... AK Fairbanks North Star
106 4 -1 ... AK Matanuska Susitna
137 3 -3 ... AK Matanuska Susitna
[11 rows x 5 columns]
How do I extract only the first column containing the indices?
python pandas dataframe
add a comment |
The command below works fine
idx = np.asarray(df.loc[df['lat1'] != '.'].ix[:,0].index)
but I'm trying to do something like this (with 2 conditions):
idx = np.asarray(df.loc[df['lat1'] != '.' and df['state'] == df['state'][0]].ix[:,0].index)
This throws the following traceback:
Traceback (most recent call last):
File "<ipython-input-274-c07cda0be195>", line 1, in <module>
idx = np.asarray(df.loc[df['lat1'] != '.' and df['state'] == df['state'][0]].ix[:,0].index)
File "/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py", line 1573, in __nonzero__
.format(self.__class__.__name__))
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I've looked it up but not able to find a suitable tweak for this case. Any leads would be appreciated.
[EDIT]: Based on the suggestion below, when I try this:
df[(df['lat1']!='.') & (df['state']== df['state'][0])]
I get
lat1 long1 ... state county
5 34 11 ... AK Anchorage
7 1 -3 ... AK Anchorage
14 1 -5 ... AK Anchorage
30 7 -3 ... AK Anchorage
44 1 -4 ... AK Anchorage
47 1 -3 ... AK Anchorage
75 1 -4 ... AK Juneau
82 5 -1 ... AK Kenai Peninsula
102 4 -1 ... AK Fairbanks North Star
106 4 -1 ... AK Matanuska Susitna
137 3 -3 ... AK Matanuska Susitna
[11 rows x 5 columns]
How do I extract only the first column containing the indices?
python pandas dataframe
The command below works fine
idx = np.asarray(df.loc[df['lat1'] != '.'].ix[:,0].index)
but I'm trying to do something like this (with 2 conditions):
idx = np.asarray(df.loc[df['lat1'] != '.' and df['state'] == df['state'][0]].ix[:,0].index)
This throws the following traceback:
Traceback (most recent call last):
File "<ipython-input-274-c07cda0be195>", line 1, in <module>
idx = np.asarray(df.loc[df['lat1'] != '.' and df['state'] == df['state'][0]].ix[:,0].index)
File "/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py", line 1573, in __nonzero__
.format(self.__class__.__name__))
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I've looked it up but not able to find a suitable tweak for this case. Any leads would be appreciated.
[EDIT]: Based on the suggestion below, when I try this:
df[(df['lat1']!='.') & (df['state']== df['state'][0])]
I get
lat1 long1 ... state county
5 34 11 ... AK Anchorage
7 1 -3 ... AK Anchorage
14 1 -5 ... AK Anchorage
30 7 -3 ... AK Anchorage
44 1 -4 ... AK Anchorage
47 1 -3 ... AK Anchorage
75 1 -4 ... AK Juneau
82 5 -1 ... AK Kenai Peninsula
102 4 -1 ... AK Fairbanks North Star
106 4 -1 ... AK Matanuska Susitna
137 3 -3 ... AK Matanuska Susitna
[11 rows x 5 columns]
How do I extract only the first column containing the indices?
python pandas dataframe
python pandas dataframe
edited Nov 21 at 1:33
asked Nov 21 at 0:34
db18
1467
1467
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I'm pretty sure this question has already been asked somewhere... But with pandas you can look at two conditions like this.
df[(df['lat1']!='.') & (df['state']== df['state'][0])]
You have to do bitwise operations
If you actually run it on a dataset, you'd find it gives an error like this: TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
– db18
Nov 21 at 1:11
If you post your dataset then it would be easier to understand. But you can probably just fill nas. df = df.fillna('')
– kradja
Nov 21 at 1:14
Please see EDIT above.
– db18
Nov 21 at 1:33
I don't understand your question. With the resulting dataframe you only want the first column containing indices? What does that mean? Also if my answer has helped you please upvote.
– kradja
Nov 21 at 5:36
I was able to solve the problem with your command. Thanks.
– db18
Nov 21 at 18:09
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%2f53403686%2ftwo-conditional-checks-inside-a-panda-data-frame-access%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I'm pretty sure this question has already been asked somewhere... But with pandas you can look at two conditions like this.
df[(df['lat1']!='.') & (df['state']== df['state'][0])]
You have to do bitwise operations
If you actually run it on a dataset, you'd find it gives an error like this: TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
– db18
Nov 21 at 1:11
If you post your dataset then it would be easier to understand. But you can probably just fill nas. df = df.fillna('')
– kradja
Nov 21 at 1:14
Please see EDIT above.
– db18
Nov 21 at 1:33
I don't understand your question. With the resulting dataframe you only want the first column containing indices? What does that mean? Also if my answer has helped you please upvote.
– kradja
Nov 21 at 5:36
I was able to solve the problem with your command. Thanks.
– db18
Nov 21 at 18:09
add a comment |
I'm pretty sure this question has already been asked somewhere... But with pandas you can look at two conditions like this.
df[(df['lat1']!='.') & (df['state']== df['state'][0])]
You have to do bitwise operations
If you actually run it on a dataset, you'd find it gives an error like this: TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
– db18
Nov 21 at 1:11
If you post your dataset then it would be easier to understand. But you can probably just fill nas. df = df.fillna('')
– kradja
Nov 21 at 1:14
Please see EDIT above.
– db18
Nov 21 at 1:33
I don't understand your question. With the resulting dataframe you only want the first column containing indices? What does that mean? Also if my answer has helped you please upvote.
– kradja
Nov 21 at 5:36
I was able to solve the problem with your command. Thanks.
– db18
Nov 21 at 18:09
add a comment |
I'm pretty sure this question has already been asked somewhere... But with pandas you can look at two conditions like this.
df[(df['lat1']!='.') & (df['state']== df['state'][0])]
You have to do bitwise operations
I'm pretty sure this question has already been asked somewhere... But with pandas you can look at two conditions like this.
df[(df['lat1']!='.') & (df['state']== df['state'][0])]
You have to do bitwise operations
answered Nov 21 at 0:52
kradja
486
486
If you actually run it on a dataset, you'd find it gives an error like this: TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
– db18
Nov 21 at 1:11
If you post your dataset then it would be easier to understand. But you can probably just fill nas. df = df.fillna('')
– kradja
Nov 21 at 1:14
Please see EDIT above.
– db18
Nov 21 at 1:33
I don't understand your question. With the resulting dataframe you only want the first column containing indices? What does that mean? Also if my answer has helped you please upvote.
– kradja
Nov 21 at 5:36
I was able to solve the problem with your command. Thanks.
– db18
Nov 21 at 18:09
add a comment |
If you actually run it on a dataset, you'd find it gives an error like this: TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
– db18
Nov 21 at 1:11
If you post your dataset then it would be easier to understand. But you can probably just fill nas. df = df.fillna('')
– kradja
Nov 21 at 1:14
Please see EDIT above.
– db18
Nov 21 at 1:33
I don't understand your question. With the resulting dataframe you only want the first column containing indices? What does that mean? Also if my answer has helped you please upvote.
– kradja
Nov 21 at 5:36
I was able to solve the problem with your command. Thanks.
– db18
Nov 21 at 18:09
If you actually run it on a dataset, you'd find it gives an error like this: TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
– db18
Nov 21 at 1:11
If you actually run it on a dataset, you'd find it gives an error like this: TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
– db18
Nov 21 at 1:11
If you post your dataset then it would be easier to understand. But you can probably just fill nas. df = df.fillna('')
– kradja
Nov 21 at 1:14
If you post your dataset then it would be easier to understand. But you can probably just fill nas. df = df.fillna('')
– kradja
Nov 21 at 1:14
Please see EDIT above.
– db18
Nov 21 at 1:33
Please see EDIT above.
– db18
Nov 21 at 1:33
I don't understand your question. With the resulting dataframe you only want the first column containing indices? What does that mean? Also if my answer has helped you please upvote.
– kradja
Nov 21 at 5:36
I don't understand your question. With the resulting dataframe you only want the first column containing indices? What does that mean? Also if my answer has helped you please upvote.
– kradja
Nov 21 at 5:36
I was able to solve the problem with your command. Thanks.
– db18
Nov 21 at 18:09
I was able to solve the problem with your command. Thanks.
– db18
Nov 21 at 18:09
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53403686%2ftwo-conditional-checks-inside-a-panda-data-frame-access%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