[英]drop duplicates of one column based on duplicates of another column keeping the other column duplicates in pandas
Keeping the duplicates of name column, I want to drop the duplicates of Count column except the unique values of name column保留 name 列的重复项,我想删除 Count 列的重复项,但 name 列的唯一值除外
here is a example df这是一个例子 df
Count![]() |
name![]() |
---|---|
yes![]() |
jhon![]() |
yes![]() |
marry![]() |
yes![]() |
marry![]() |
yes![]() |
ishita![]() |
yes![]() |
ishita![]() |
yes![]() |
ishita![]() |
The result I want as:我想要的结果是:
Count![]() |
name![]() |
---|---|
yes![]() |
jhon![]() |
yes![]() |
marry![]() |
marry![]() |
|
yes![]() |
ishita![]() |
ishita![]() |
|
ishita![]() |
#pandas #python #熊猫#蟒蛇
The logic is逻辑是
groupby()
and cumcount()
instances of Name groupby()
和cumcount()
实例df = pd.read_csv(io.StringIO("""Count name
yes jhon
yes marry
yes marry
yes ishita
yes ishita
yes ishita"""),sep="\t")
df.Count=np.where(df.groupby("name",as_index=False)["name"].cumcount()==0, df.Count, np.nan)
Count![]() |
name![]() |
|
---|---|---|
0 ![]() |
yes![]() |
jhon![]() |
1 ![]() |
yes![]() |
marry![]() |
2 ![]() |
nan![]() |
marry![]() |
3 ![]() |
yes![]() |
ishita![]() |
4 ![]() |
nan![]() |
ishita![]() |
5 ![]() |
nan![]() |
ishita![]() |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.