简体   繁体   中英

Pandas: get rows with consecutive column values and add a couter row

I need to go through a large pd and select consecutive rows with similar values in a column. ie in the pd below and selecting column x:

col row x   y
1   1   1   1
2   2   2   2
6   3   3   8
9   2   3   4
5   3   3   9
4   9   4   4
5   5   5   1
3   7   5   2
6   6   6   6

The results output would be:

col row x   y
6   3   3   8
9   2   3   4
5   3   3   9
5   5   5   1
3   7   5   2

Not sure how to do this.

IIUC, use boolean indexing using a mask of the consecutive values:

m = df['x'].eq(df['x'].shift())
df[m|m.shift(-1, fill_value=False)]

Output:

   col  row  x  y
2    6    3  3  8
3    9    2  3  4
4    5    3  3  9
6    5    5  5  1
7    3    7  5  2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM