遍历 df 并使用条件删除不需要的行

Looping through df and using a conditional to remove unneeded rows

抱歉,如果这可能是一个重复的问题,我没有运气在半相似帖子的指导下解决我的问题。

我有一个带有列 ID 和 current_stage 的 df,使用 Python

我想遍历并找到 ID 中的重复值,并在重复的值中检查当前阶段是否有 1 或 2。如果他们只有 1 或 2,那么我只需要该 ID 的一个记录。如果重复 ID 的实例中有 3 或 4,我想保留该重复 ID 的所有记录。

感谢堆栈溢出之神的帮助!

谢谢

我可能有办法绕过去..

您将数据分成两个数据帧,从一个数据帧中删除重复项并再次合并它们,如下所示:

df1 = df[df['current_stage'].isin([1,2])]
df2 = df[~df['current_stage'].isin([1,2])]
df1.drop_duplicates(subset=['ID'], inplace = True)
df = pd.concat([df1, df2])

给定以下数据框:

df = pd.DataFrame({'ID': [1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3], 'current_stage': [1, 1, 2, 3, 3, 4, 4, 1, 2, 2, 4]})

    ID  current_stage
0    1              1
1    1              1
2    1              2
3    1              3
4    1              3
5    2              4
6    2              4
7    2              1
8    2              2
9    2              2
10   3              4

你可以这样做:

out = df[df.groupby('ID')['current_stage'].transform(np.size)>1].groupby('current_stage').apply(lambda x: x.iloc[0].to_frame().T if x.iloc[0]['current_stage'] in [1,2] else x).reset_index(drop=True)

输出:

   ID  current_stage
0   1              1
1   1              2
2   1              3
3   1              3
4   2              4
5   2              4