In Pandas how can I reduce the rows so that I only accept the row with the first true in every sub group of...
As an example, consider the following:
Input
a b c bool
1 243 0 true
1 253 1 false
1 267 0 true
1 245 0 true
1 234 0 false
1 255 0 true
1 275 0 true
1 295 0 true
I want the output to be the following:
a b c bool
1 243 0 true
1 253 1 false
1 267 0 true
1 234 0 false
1 255 0 true
python pandas
add a comment |
As an example, consider the following:
Input
a b c bool
1 243 0 true
1 253 1 false
1 267 0 true
1 245 0 true
1 234 0 false
1 255 0 true
1 275 0 true
1 295 0 true
I want the output to be the following:
a b c bool
1 243 0 true
1 253 1 false
1 267 0 true
1 234 0 false
1 255 0 true
python pandas
add a comment |
As an example, consider the following:
Input
a b c bool
1 243 0 true
1 253 1 false
1 267 0 true
1 245 0 true
1 234 0 false
1 255 0 true
1 275 0 true
1 295 0 true
I want the output to be the following:
a b c bool
1 243 0 true
1 253 1 false
1 267 0 true
1 234 0 false
1 255 0 true
python pandas
As an example, consider the following:
Input
a b c bool
1 243 0 true
1 253 1 false
1 267 0 true
1 245 0 true
1 234 0 false
1 255 0 true
1 275 0 true
1 295 0 true
I want the output to be the following:
a b c bool
1 243 0 true
1 253 1 false
1 267 0 true
1 234 0 false
1 255 0 true
python pandas
python pandas
asked Nov 14 '18 at 23:09
NothingNothing
5211
5211
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Use some boolean masking for this and do it in one line:
df.loc[(df['bool'] != df['bool'].shift(1))]
out:
>>> df.loc[(df['bool'] != df['bool'].shift(1))]
a b c bool
0 1 243 0 True
1 1 253 1 False
2 1 267 0 True
4 1 234 0 False
5 1 255 0 True
add a comment |
IIUC
df.groupby(df['bool'].ne(True).cumsum()).head(2)
Out[201]:
a b c bool
0 1 243 0 True
1 1 253 1 False
2 1 267 0 True
4 1 234 0 False
5 1 255 0 True
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%2f53310109%2fin-pandas-how-can-i-reduce-the-rows-so-that-i-only-accept-the-row-with-the-first%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use some boolean masking for this and do it in one line:
df.loc[(df['bool'] != df['bool'].shift(1))]
out:
>>> df.loc[(df['bool'] != df['bool'].shift(1))]
a b c bool
0 1 243 0 True
1 1 253 1 False
2 1 267 0 True
4 1 234 0 False
5 1 255 0 True
add a comment |
Use some boolean masking for this and do it in one line:
df.loc[(df['bool'] != df['bool'].shift(1))]
out:
>>> df.loc[(df['bool'] != df['bool'].shift(1))]
a b c bool
0 1 243 0 True
1 1 253 1 False
2 1 267 0 True
4 1 234 0 False
5 1 255 0 True
add a comment |
Use some boolean masking for this and do it in one line:
df.loc[(df['bool'] != df['bool'].shift(1))]
out:
>>> df.loc[(df['bool'] != df['bool'].shift(1))]
a b c bool
0 1 243 0 True
1 1 253 1 False
2 1 267 0 True
4 1 234 0 False
5 1 255 0 True
Use some boolean masking for this and do it in one line:
df.loc[(df['bool'] != df['bool'].shift(1))]
out:
>>> df.loc[(df['bool'] != df['bool'].shift(1))]
a b c bool
0 1 243 0 True
1 1 253 1 False
2 1 267 0 True
4 1 234 0 False
5 1 255 0 True
answered Nov 14 '18 at 23:18
d_kennetzd_kennetz
2,2803724
2,2803724
add a comment |
add a comment |
IIUC
df.groupby(df['bool'].ne(True).cumsum()).head(2)
Out[201]:
a b c bool
0 1 243 0 True
1 1 253 1 False
2 1 267 0 True
4 1 234 0 False
5 1 255 0 True
add a comment |
IIUC
df.groupby(df['bool'].ne(True).cumsum()).head(2)
Out[201]:
a b c bool
0 1 243 0 True
1 1 253 1 False
2 1 267 0 True
4 1 234 0 False
5 1 255 0 True
add a comment |
IIUC
df.groupby(df['bool'].ne(True).cumsum()).head(2)
Out[201]:
a b c bool
0 1 243 0 True
1 1 253 1 False
2 1 267 0 True
4 1 234 0 False
5 1 255 0 True
IIUC
df.groupby(df['bool'].ne(True).cumsum()).head(2)
Out[201]:
a b c bool
0 1 243 0 True
1 1 253 1 False
2 1 267 0 True
4 1 234 0 False
5 1 255 0 True
answered Nov 14 '18 at 23:20
Wen-BenWen-Ben
112k83367
112k83367
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%2f53310109%2fin-pandas-how-can-i-reduce-the-rows-so-that-i-only-accept-the-row-with-the-first%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