擅长:python、mysql、java
<p>假设您的数据集是变量“df”中的数据帧。可能有一种更有效的方法,但这是可行的:</p>
<pre><code>ok_records = list(df['ID'][df['RULE'] == 'OK'])
df1 = df[(df['ID'].isin(ok_records)) & (df['RULE'] != 'NOT_OK')]
df2 = df[(df['RULE'] == 'NOT_OK') & (~df['ID'].isin(ok_records))]
final_df = pd.concat([df1, df2])
</code></pre>