Pandas - Delete all consecutive rows except the first one which share same column value [duplicate]












1















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?










share|improve this question













marked as duplicate by jezrael dataframe
Users with the  dataframe badge can single-handedly close dataframe questions as duplicates and reopen them as needed.

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
















1















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?










share|improve this question













marked as duplicate by jezrael dataframe
Users with the  dataframe badge can single-handedly close dataframe questions as duplicates and reopen them as needed.

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














1












1








1








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 13:00









Lokesh

68831122




68831122




marked as duplicate by jezrael dataframe
Users with the  dataframe badge can single-handedly close dataframe questions as duplicates and reopen them as needed.

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 dataframe
Users with the  dataframe badge can single-handedly close dataframe questions as duplicates and reopen them as needed.

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


















  • @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












1 Answer
1






active

oldest

votes


















2














Use -



df.loc[df['D'].shift(1) != df['D']]


Output



    A   B   C   D
0 h e l 0
3 q u e 1





share|improve this answer





















  • 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






  • 2




    Thanks for the great answer. Working without a problem! You rock
    – Lokesh
    Nov 19 '18 at 13:14




















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Use -



df.loc[df['D'].shift(1) != df['D']]


Output



    A   B   C   D
0 h e l 0
3 q u e 1





share|improve this answer





















  • 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






  • 2




    Thanks for the great answer. Working without a problem! You rock
    – Lokesh
    Nov 19 '18 at 13:14


















2














Use -



df.loc[df['D'].shift(1) != df['D']]


Output



    A   B   C   D
0 h e l 0
3 q u e 1





share|improve this answer





















  • 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






  • 2




    Thanks for the great answer. Working without a problem! You rock
    – Lokesh
    Nov 19 '18 at 13:14
















2












2








2






Use -



df.loc[df['D'].shift(1) != df['D']]


Output



    A   B   C   D
0 h e l 0
3 q u e 1





share|improve this answer












Use -



df.loc[df['D'].shift(1) != df['D']]


Output



    A   B   C   D
0 h e l 0
3 q u e 1






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 13:04









Vivek Kalyanarangan

4,7761826




4,7761826












  • 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






  • 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










  • @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





Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]