pandas DataFrame isin and following row
For a given DataFrame, sorted by b
and index reset:
df = pd.DataFrame('a': list('abcdef'),
'b': [0, 2, 7, 3, 9, 15]
).sort_values('b').reset_index(drop=True)
a b
0 a 0
1 b 2
2 d 3
3 c 7
4 e 9
5 f 15
and a list, v
v = list('adf')
I would like to pull out just the rows in v
and the following row (if there is one), similar to grep -A1
:
a b
0 a 0
1 b 2
2 d 3
3 c 7
5 f 15
I can do this by concatenating the index from isin
and the index from isin
plus one, like so:
df[df.index.isin(
np.concatenate(
(df[df['a'].isin(v)].index,
df[df['a'].isin(v)].index + 1)))]
But this is long and not too easy to understand. Is there a better way?
python pandas
add a comment |
For a given DataFrame, sorted by b
and index reset:
df = pd.DataFrame('a': list('abcdef'),
'b': [0, 2, 7, 3, 9, 15]
).sort_values('b').reset_index(drop=True)
a b
0 a 0
1 b 2
2 d 3
3 c 7
4 e 9
5 f 15
and a list, v
v = list('adf')
I would like to pull out just the rows in v
and the following row (if there is one), similar to grep -A1
:
a b
0 a 0
1 b 2
2 d 3
3 c 7
5 f 15
I can do this by concatenating the index from isin
and the index from isin
plus one, like so:
df[df.index.isin(
np.concatenate(
(df[df['a'].isin(v)].index,
df[df['a'].isin(v)].index + 1)))]
But this is long and not too easy to understand. Is there a better way?
python pandas
add a comment |
For a given DataFrame, sorted by b
and index reset:
df = pd.DataFrame('a': list('abcdef'),
'b': [0, 2, 7, 3, 9, 15]
).sort_values('b').reset_index(drop=True)
a b
0 a 0
1 b 2
2 d 3
3 c 7
4 e 9
5 f 15
and a list, v
v = list('adf')
I would like to pull out just the rows in v
and the following row (if there is one), similar to grep -A1
:
a b
0 a 0
1 b 2
2 d 3
3 c 7
5 f 15
I can do this by concatenating the index from isin
and the index from isin
plus one, like so:
df[df.index.isin(
np.concatenate(
(df[df['a'].isin(v)].index,
df[df['a'].isin(v)].index + 1)))]
But this is long and not too easy to understand. Is there a better way?
python pandas
For a given DataFrame, sorted by b
and index reset:
df = pd.DataFrame('a': list('abcdef'),
'b': [0, 2, 7, 3, 9, 15]
).sort_values('b').reset_index(drop=True)
a b
0 a 0
1 b 2
2 d 3
3 c 7
4 e 9
5 f 15
and a list, v
v = list('adf')
I would like to pull out just the rows in v
and the following row (if there is one), similar to grep -A1
:
a b
0 a 0
1 b 2
2 d 3
3 c 7
5 f 15
I can do this by concatenating the index from isin
and the index from isin
plus one, like so:
df[df.index.isin(
np.concatenate(
(df[df['a'].isin(v)].index,
df[df['a'].isin(v)].index + 1)))]
But this is long and not too easy to understand. Is there a better way?
python pandas
python pandas
asked Nov 15 '18 at 15:31
AlexAlex
1,063822
1,063822
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can combine the isin
condition and the shift
(next row) to create the boolean you needed:
df[df.a.isin(v).pipe(lambda x: x | x.shift())]
# a b
#0 a 0
#1 b 2
#2 d 3
#3 c 7
#5 f 15
2
Nice answer: to note, this is functionally equivalent tox = df.a.isin(v); x | x.shift()
.pipe
is here just for convenience.
– jpp
Nov 15 '18 at 15:40
So what isx | x.shift()
doing here? Is thisx OR x.shift()
? @jpp
– Alex
Nov 15 '18 at 15:47
1
Yes.x
is a Boolean series,|
means vectorised "or".
– jpp
Nov 15 '18 at 15:48
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%2f53322764%2fpandas-dataframe-isin-and-following-row%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
You can combine the isin
condition and the shift
(next row) to create the boolean you needed:
df[df.a.isin(v).pipe(lambda x: x | x.shift())]
# a b
#0 a 0
#1 b 2
#2 d 3
#3 c 7
#5 f 15
2
Nice answer: to note, this is functionally equivalent tox = df.a.isin(v); x | x.shift()
.pipe
is here just for convenience.
– jpp
Nov 15 '18 at 15:40
So what isx | x.shift()
doing here? Is thisx OR x.shift()
? @jpp
– Alex
Nov 15 '18 at 15:47
1
Yes.x
is a Boolean series,|
means vectorised "or".
– jpp
Nov 15 '18 at 15:48
add a comment |
You can combine the isin
condition and the shift
(next row) to create the boolean you needed:
df[df.a.isin(v).pipe(lambda x: x | x.shift())]
# a b
#0 a 0
#1 b 2
#2 d 3
#3 c 7
#5 f 15
2
Nice answer: to note, this is functionally equivalent tox = df.a.isin(v); x | x.shift()
.pipe
is here just for convenience.
– jpp
Nov 15 '18 at 15:40
So what isx | x.shift()
doing here? Is thisx OR x.shift()
? @jpp
– Alex
Nov 15 '18 at 15:47
1
Yes.x
is a Boolean series,|
means vectorised "or".
– jpp
Nov 15 '18 at 15:48
add a comment |
You can combine the isin
condition and the shift
(next row) to create the boolean you needed:
df[df.a.isin(v).pipe(lambda x: x | x.shift())]
# a b
#0 a 0
#1 b 2
#2 d 3
#3 c 7
#5 f 15
You can combine the isin
condition and the shift
(next row) to create the boolean you needed:
df[df.a.isin(v).pipe(lambda x: x | x.shift())]
# a b
#0 a 0
#1 b 2
#2 d 3
#3 c 7
#5 f 15
edited Nov 15 '18 at 15:40
answered Nov 15 '18 at 15:36
PsidomPsidom
127k1293138
127k1293138
2
Nice answer: to note, this is functionally equivalent tox = df.a.isin(v); x | x.shift()
.pipe
is here just for convenience.
– jpp
Nov 15 '18 at 15:40
So what isx | x.shift()
doing here? Is thisx OR x.shift()
? @jpp
– Alex
Nov 15 '18 at 15:47
1
Yes.x
is a Boolean series,|
means vectorised "or".
– jpp
Nov 15 '18 at 15:48
add a comment |
2
Nice answer: to note, this is functionally equivalent tox = df.a.isin(v); x | x.shift()
.pipe
is here just for convenience.
– jpp
Nov 15 '18 at 15:40
So what isx | x.shift()
doing here? Is thisx OR x.shift()
? @jpp
– Alex
Nov 15 '18 at 15:47
1
Yes.x
is a Boolean series,|
means vectorised "or".
– jpp
Nov 15 '18 at 15:48
2
2
Nice answer: to note, this is functionally equivalent to
x = df.a.isin(v); x | x.shift()
. pipe
is here just for convenience.– jpp
Nov 15 '18 at 15:40
Nice answer: to note, this is functionally equivalent to
x = df.a.isin(v); x | x.shift()
. pipe
is here just for convenience.– jpp
Nov 15 '18 at 15:40
So what is
x | x.shift()
doing here? Is this x OR x.shift()
? @jpp– Alex
Nov 15 '18 at 15:47
So what is
x | x.shift()
doing here? Is this x OR x.shift()
? @jpp– Alex
Nov 15 '18 at 15:47
1
1
Yes.
x
is a Boolean series, |
means vectorised "or".– jpp
Nov 15 '18 at 15:48
Yes.
x
is a Boolean series, |
means vectorised "or".– jpp
Nov 15 '18 at 15:48
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%2f53322764%2fpandas-dataframe-isin-and-following-row%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