我是熊猫新手。我一直试图在这里解决一个问题
这是我想要的输出类型
IIUC,这就是你需要的
a = (df['A'].ne(df['A'].shift())).ne((df['B'].ne(df['B'].shift())))
df[~a].reset_index(drop=True)
输出
A B
0 2 z
1 3 x
2 3 x
我认为你需要:
cond=(df.eq(df.shift(-1))|df.eq(df.shift())).all(axis=1)
pd.concat([df[~cond].groupby('A').last().reset_index(),df[cond]])
A B
0 2 y
2 3 x
3 3 x