inactive
ステータスで一意の値をフィルタリングする方法を探していますがactive
、同じ一意の値でステータスとして繰り返されることはありません。
df:
Unique_value Status
1 Active <- Has both active and inactive, must be inactive only
1 Active <- Has both active and inactive, must be inactive only
1 Inactive <- Has both active and inactive, must be inactive only
1 Inactive <- Has both active and inactive, must be inactive only
2 Inactive <- Has inactive only
2 Inactive <- Has inactive only
2 Inactive <- Has inactive only
3 Inactive <- Has inactive only (cancelled okay to be filtered out)
3 Cancelled <- Has inactive only (cancelled okay to be filtered out)
3 Inactive <- Has inactive only (cancelled okay to be filtered out)
必要な出力:
Unique_value status
2 Inactive
3 Inactive
私がこれまでに試したことですが、これは正しくないと思います。
p = ['Inactive', 'Active']
df.groupby('Unique_value')['Status'].apply(lambda x: (x =='Inactive') != set(p))
もし最初のチェックany
、各グループ内の値のですActive
かInactive
。次に、両方の条件が当てはまるグループを削除します。
m1 = df["Status"].eq("Active").groupby(df["Unique_value"]).transform("any")
m2 = df["Status"].eq("Inactive").groupby(df["Unique_value"]).transform("any")
df[~(m1 & m2)].groupby("Unique_value", as_index=False).first()
Unique_value Status
0 2 Inactive
1 3 Inactive
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加