Pandas - Delete all consecutive rows except the first one which share same column value [duplicate]
This question already has an answer here:
Remove duplicate rows from Pandas dataframe where only some columns have the same value
1 answer
Pandas: Drop consecutive duplicates
3 answers
I have a Data-Frame like following:
A B C D
- - - -
h e l 0
t h i 0
i s m 0
q u e 1
s t i 1
I want to delete all the rows that have same value in D
consecutively but leaving the first one. The result will be following:
A B C D
- - - -
h e l 0
q u e 1
So, far I've done it using following code:
df[list(map(lambda x: (x == 0) or (df['D'][x] != df['D'][x-1]), range(len(D))))]
I'm wondering if there a better way to do this?
pandas dataframe
marked as duplicate by jezrael
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 '18 at 13:04
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:
Remove duplicate rows from Pandas dataframe where only some columns have the same value
1 answer
Pandas: Drop consecutive duplicates
3 answers
I have a Data-Frame like following:
A B C D
- - - -
h e l 0
t h i 0
i s m 0
q u e 1
s t i 1
I want to delete all the rows that have same value in D
consecutively but leaving the first one. The result will be following:
A B C D
- - - -
h e l 0
q u e 1
So, far I've done it using following code:
df[list(map(lambda x: (x == 0) or (df['D'][x] != df['D'][x-1]), range(len(D))))]
I'm wondering if there a better way to do this?
pandas dataframe
marked as duplicate by jezrael
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 '18 at 13:04
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.
@jezrael this looks different than the one you marked duplicate against. The OP wants consecutive duplicates to be removed
– Vivek Kalyanarangan
Nov 19 '18 at 13:06
1
@VivekKalyanarangan - added new dupe...
– jezrael
Nov 19 '18 at 13:09
add a comment |
This question already has an answer here:
Remove duplicate rows from Pandas dataframe where only some columns have the same value
1 answer
Pandas: Drop consecutive duplicates
3 answers
I have a Data-Frame like following:
A B C D
- - - -
h e l 0
t h i 0
i s m 0
q u e 1
s t i 1
I want to delete all the rows that have same value in D
consecutively but leaving the first one. The result will be following:
A B C D
- - - -
h e l 0
q u e 1
So, far I've done it using following code:
df[list(map(lambda x: (x == 0) or (df['D'][x] != df['D'][x-1]), range(len(D))))]
I'm wondering if there a better way to do this?
pandas dataframe
This question already has an answer here:
Remove duplicate rows from Pandas dataframe where only some columns have the same value
1 answer
Pandas: Drop consecutive duplicates
3 answers
I have a Data-Frame like following:
A B C D
- - - -
h e l 0
t h i 0
i s m 0
q u e 1
s t i 1
I want to delete all the rows that have same value in D
consecutively but leaving the first one. The result will be following:
A B C D
- - - -
h e l 0
q u e 1
So, far I've done it using following code:
df[list(map(lambda x: (x == 0) or (df['D'][x] != df['D'][x-1]), range(len(D))))]
I'm wondering if there a better way to do this?
This question already has an answer here:
Remove duplicate rows from Pandas dataframe where only some columns have the same value
1 answer
Pandas: Drop consecutive duplicates
3 answers
pandas dataframe
pandas dataframe
asked Nov 19 '18 at 13:00
Lokesh
68831122
68831122
marked as duplicate by jezrael
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 '18 at 13:04
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 jezrael
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 '18 at 13:04
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.
@jezrael this looks different than the one you marked duplicate against. The OP wants consecutive duplicates to be removed
– Vivek Kalyanarangan
Nov 19 '18 at 13:06
1
@VivekKalyanarangan - added new dupe...
– jezrael
Nov 19 '18 at 13:09
add a comment |
@jezrael this looks different than the one you marked duplicate against. The OP wants consecutive duplicates to be removed
– Vivek Kalyanarangan
Nov 19 '18 at 13:06
1
@VivekKalyanarangan - added new dupe...
– jezrael
Nov 19 '18 at 13:09
@jezrael this looks different than the one you marked duplicate against. The OP wants consecutive duplicates to be removed
– Vivek Kalyanarangan
Nov 19 '18 at 13:06
@jezrael this looks different than the one you marked duplicate against. The OP wants consecutive duplicates to be removed
– Vivek Kalyanarangan
Nov 19 '18 at 13:06
1
1
@VivekKalyanarangan - added new dupe...
– jezrael
Nov 19 '18 at 13:09
@VivekKalyanarangan - added new dupe...
– jezrael
Nov 19 '18 at 13:09
add a comment |
1 Answer
1
active
oldest
votes
Use -
df.loc[df['D'].shift(1) != df['D']]
Output
A B C D
0 h e l 0
3 q u e 1
I fear this won't work if0
occurs more than 2 times. Right?
– Lokesh
Nov 19 '18 at 13:06
@Lokesh didn't get you. I tried it against OP's example which contains 3 consecutive duplicates and it worked for me
– Vivek Kalyanarangan
Nov 19 '18 at 13:07
2
Thanks for the great answer. Working without a problem! You rock
– Lokesh
Nov 19 '18 at 13:14
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use -
df.loc[df['D'].shift(1) != df['D']]
Output
A B C D
0 h e l 0
3 q u e 1
I fear this won't work if0
occurs more than 2 times. Right?
– Lokesh
Nov 19 '18 at 13:06
@Lokesh didn't get you. I tried it against OP's example which contains 3 consecutive duplicates and it worked for me
– Vivek Kalyanarangan
Nov 19 '18 at 13:07
2
Thanks for the great answer. Working without a problem! You rock
– Lokesh
Nov 19 '18 at 13:14
add a comment |
Use -
df.loc[df['D'].shift(1) != df['D']]
Output
A B C D
0 h e l 0
3 q u e 1
I fear this won't work if0
occurs more than 2 times. Right?
– Lokesh
Nov 19 '18 at 13:06
@Lokesh didn't get you. I tried it against OP's example which contains 3 consecutive duplicates and it worked for me
– Vivek Kalyanarangan
Nov 19 '18 at 13:07
2
Thanks for the great answer. Working without a problem! You rock
– Lokesh
Nov 19 '18 at 13:14
add a comment |
Use -
df.loc[df['D'].shift(1) != df['D']]
Output
A B C D
0 h e l 0
3 q u e 1
Use -
df.loc[df['D'].shift(1) != df['D']]
Output
A B C D
0 h e l 0
3 q u e 1
answered Nov 19 '18 at 13:04
Vivek Kalyanarangan
4,7761826
4,7761826
I fear this won't work if0
occurs more than 2 times. Right?
– Lokesh
Nov 19 '18 at 13:06
@Lokesh didn't get you. I tried it against OP's example which contains 3 consecutive duplicates and it worked for me
– Vivek Kalyanarangan
Nov 19 '18 at 13:07
2
Thanks for the great answer. Working without a problem! You rock
– Lokesh
Nov 19 '18 at 13:14
add a comment |
I fear this won't work if0
occurs more than 2 times. Right?
– Lokesh
Nov 19 '18 at 13:06
@Lokesh didn't get you. I tried it against OP's example which contains 3 consecutive duplicates and it worked for me
– Vivek Kalyanarangan
Nov 19 '18 at 13:07
2
Thanks for the great answer. Working without a problem! You rock
– Lokesh
Nov 19 '18 at 13:14
I fear this won't work if
0
occurs more than 2 times. Right?– Lokesh
Nov 19 '18 at 13:06
I fear this won't work if
0
occurs more than 2 times. Right?– Lokesh
Nov 19 '18 at 13:06
@Lokesh didn't get you. I tried it against OP's example which contains 3 consecutive duplicates and it worked for me
– Vivek Kalyanarangan
Nov 19 '18 at 13:07
@Lokesh didn't get you. I tried it against OP's example which contains 3 consecutive duplicates and it worked for me
– Vivek Kalyanarangan
Nov 19 '18 at 13:07
2
2
Thanks for the great answer. Working without a problem! You rock
– Lokesh
Nov 19 '18 at 13:14
Thanks for the great answer. Working without a problem! You rock
– Lokesh
Nov 19 '18 at 13:14
add a comment |
@jezrael this looks different than the one you marked duplicate against. The OP wants consecutive duplicates to be removed
– Vivek Kalyanarangan
Nov 19 '18 at 13:06
1
@VivekKalyanarangan - added new dupe...
– jezrael
Nov 19 '18 at 13:09