如何根据同一列中的另一个值更新列中的值,其中两行在另一列中具有相同的值?

How to update the value in a column based on another value in the same column where both rows have the same value in another column?

数据帧 df 给出:

    ID    I   J   K
0   10    1   a   1
1   10    2   b   nan
2   10    3   c   nan
3   11    1   f   0
4   11    2   b   nan
5   11    3   d   nan
6   12    1   b   1
7   12    2   d   nan
8   12    3   c   nan

对于 ID 中的每个唯一值,当 I==3 时,如果 J=='c',则 K=1,其中 I==1,否则 K=0 . K 中的其他值无关紧要。也就是说,第0、3、6行的K的值分别由第2、5、8行的I的值决定。

尝试:

IDs = df.loc[df.I.eq(3) & df.J.eq("c"), "ID"]
df["K"] = np.where(df["ID"].isin(IDs) & df.I.eq(1), 1, 0)
df["K"] = np.where(df.I.eq(1), df.K, np.nan) # <-- if you want other values NaNs
print(df)

打印:

   ID  I  J    K
0  10  1  a  1.0
1  10  2  b  NaN
2  10  3  c  NaN
3  11  1  f  0.0
4  11  2  b  NaN
5  11  3  d  NaN
6  12  1  b  1.0
7  12  2  d  NaN
8  12  3  c  NaN