[英]Filter a row based on a certain condition from a grouped data - python
您可以按列b_name
進行分組,然后使用filter
僅保留那些同時在p_f
列中具有F
和P
值的組(對於每個組)。 接下來,使用drop_duplicates("b_name")
刪除重復的行並將p_f
設置為所需的 output。
import pandas as pd
df = pd.read_csv("sample.csv", sep=";")
print(df)
df_group = df.groupby("b_name")
df_filter = df_group.filter(lambda x:
("F" in x.p_f.values) and ("P" in x.p_f.values)
)
df_filter = df_filter.drop_duplicates("b_name")
df_filter["p_f"] = "FP"
print(df_filter[["b_id", "b_name", "p_f"]])
Output 來自df_filter
b_id b_name p_f
0 29743 XXXX FP
3 29751 ZZZZ FP
6 30832 BBBB FP
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.