[英]Drop duplicates of one column based on value in another column, Python, Pandas
[英]drop duplicates of one column based on duplicates of another column keeping the other column duplicates in pandas
保留 name 列的重復項,我想刪除 Count 列的重復項,但 name 列的唯一值除外
這是一個例子 df
數數 | 姓名 |
---|---|
是的 | 約翰 |
是的 | 結婚 |
是的 | 結婚 |
是的 | 石田 |
是的 | 石田 |
是的 | 石田 |
我想要的結果是:
數數 | 姓名 |
---|---|
是的 | 約翰 |
是的 | 結婚 |
結婚 | |
是的 | 石田 |
石田 | |
石田 |
#熊貓#蟒蛇
邏輯是
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)
數數 | 姓名 | |
---|---|---|
0 | 是的 | 約翰 |
1 | 是的 | 結婚 |
2 | 楠 | 結婚 |
3 | 是的 | 石田 |
4 | 楠 | 石田 |
5 | 楠 | 石田 |
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.