简体   繁体   English

根据另一列的重复项删除一列的重复项,将另一列重复项保留在 pandas

[英]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 namegroupby()cumcount()实例
  • 0th instance, keep Count otherwise set to NaN第 0 个实例,保持Count否则设置为NaN
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 0 yes是的 jhon约翰
1 1 yes是的 marry结婚
2 2 nan marry结婚
3 3 yes是的 ishita石田
4 4 nan ishita石田
5 5 nan ishita石田

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据另一列(Python,Pandas)中的值删除一列的重复项 - Drop duplicates of one column based on value in another column, Python, Pandas pandas - 在一列中删除重复项,计算重复项的数量并聚合一列 - pandas - drop duplicates in a column, count the number of duplicates and aggregate one column 基于列条件删除重复 Pandas - Drop Duplicates Based On Column Conditional Pandas Pandas dataframe 删除基于另一列值的重复项 - Pandas dataframe drop duplicates based in another column value 熊猫使用条件删除一列的重复项 - pandas drop duplicates of one column with criteria 在熊猫数据框中转换列,同时保持其他列完整无缺 - Transposing a column in a pandas dataframe while keeping other column intact with duplicates 如何根据 DataFrame Python Pandas 中其他 2 列中的值删除一列中的重复项? - How to drop duplicates in one column based on values in 2 other columns in DataFrame in Python Pandas? 如何删除与 pandas 中另一列中的值相关的列中的重复项? - How to drop duplicates in column with respect to values in another column in pandas? 删除重复项,保留另一列中具有最高值的行 - Drop duplicates keeping the row with the highest value in another column 将重复项放在一列上,打破另一列的联系 - Drop duplicates on one column, breaking ties from another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM