Pandas 重复值

Pandas 重复值,pandas,pandas-groupby,pandasql,pandas-apply,pandas-profiling,Pandas,Pandas Groupby,Pandasql,Pandas Apply,Pandas Profiling,我对熊猫不熟悉。 我一直在努力解决一个问题 这是我想要的输出 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 IIUC,这就是你需要的 a = (df['A'].ne(df['A'].shift())).ne((df['B'].ne(

我对熊猫不熟悉。 我一直在努力解决一个问题

这是我想要的输出


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

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
我认为你需要:

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

这不是预期的结果output@Krishan,你能澄清一下吗?根据您的描述,我的代码保留第一行,但是,您的图片似乎保留第二行。你能澄清哪一个是正确的吗?这不是预期的结果output@Krishan,你能澄清一下吗?根据您的描述,我的代码保留第一行,但是,您的图片似乎保留第二行。你能澄清哪一个是正确的吗?