【问题标题】:How to identify and set a column value for only the last occurrence of a duplicate row如何仅为最后一次出现的重复行识别和设置列值
【发布时间】:2018-03-09 15:45:04
【问题描述】:

我对 Pandas 和 Python 很陌生,如果这是一个基本问题,请原谅我。为了解决我的问题:Load multiple csv files, look for missing merchandiseID in subsequent files, calculate the date sold based on it,我对清理这些文件的方式进行了一些更改。我在从多个 csv 文件加载的数据框中有以下列。

store_id stock_number merchandise_id date_acquired color price MSRP csv_date
12973     7382        UISN78008     04/11/2017    Red  $3200 $3650  01/31/2017
45973     9889        YHAN79807     08/09/2017   White $3600 $3650  01/31/2017
...
45973     9889        YHAN79807     08/09/2017   White $3600 $3650  03/31/2017

最后一列是商品 ID 为“YHAN79807”的商品的最后一次出现。通过关注How to identify the first occurence of duplicate rows in Python pandas Dataframe 并对其进行一些修改,我能够找到最后一次出现。我用过

 df1['dup_index'] = df1.index.map(lambda ind: g.indices[ind][len(g.indices[ind])-1])

但是,我只想为最后一次出现的“YHAN79807”设置“dup_index”列的值作为商品ID。我不希望将 'YHAN79807' 作为商品 ID 的重复数据的其余行具有此值。它们应该是空白的。只有最后一次出现应该有这个 ID。我还不能这样做。我尝试了几件事,其中之一是:

group = df1.groupby(['merchandiseID'])
df1_index = df1.set_index(['merchandiseID'])
df1[ (((len(group.indices[ind])-1)==group.indices[df1.merchandiseID])]['dup_index'] = 'succeed'

我尝试添加“成功”作为第一步,看看列比较是否会给我结果,但它给了我以下错误:

 FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison

结果 = getattr(x, name)(y) ... raise TypeError('Could not compare %s type with Series' %

我束手无策。我错过了什么?任何指针表示赞赏。

最好的,

爱丽丝

【问题讨论】:

  • 我的解决方案如何工作?还是需要别的东西?
  • 非常感谢@jezrael。我会在当天晚些时候尝试这个,并肯定会让你知道。再次感谢。

标签: python pandas


【解决方案1】:

我认为你需要:

g = df.groupby(['merchandise_id'])
df1 = df.set_index(['merchandise_id'])
df['dup_index'] = df1.index.map(lambda ind: g.indices[ind][len(g.indices[ind])-1])
print (df)
   store_id  stock_number merchandise_id date_acquired  color  price   MSRP  \
0     12973          7382      UISN78008    04/11/2017    Red  $3200  $3650   
1     45973          9889      YHAN79807    08/09/2017  White  $3600  $3650   
2     45973          9889      YHAN79807    08/09/2017  White  $3600  $3650   

     csv_date  dup_index  
0  01/31/2017          0  
1  01/31/2017          2  
2  03/31/2017          2  

或者如果需要仅识别最后重复的行,请使用 & 的双重条件:

print (df)
   store_id  stock_number merchandise_id date_acquired  color  price   MSRP  \
0     12973          7382      UISN78008    04/11/2017    Red  $3200  $3650   
1     45973          9889      YHAN79807    08/09/2017  White  $3600  $3650   
2     45973          9889      YHAN79807    08/09/2017  White  $3600  $3650   
3     45973          9889      YHAN79807    08/09/2017  White  $3600  $3650   

     csv_date  
0  01/31/2017  
1  01/31/2017  
2  01/31/2017  
3  03/31/2017  


m1 = ~df.duplicated(['merchandise_id'], keep='last')
m2 = df.duplicated(['merchandise_id'], keep=False)
m = m1 & m2
df.loc[m, 'new'] = 'succeed'
print (df)
   store_id  stock_number merchandise_id date_acquired  color  price   MSRP  \
0     12973          7382      UISN78008    04/11/2017    Red  $3200  $3650   
1     45973          9889      YHAN79807    08/09/2017  White  $3600  $3650   
2     45973          9889      YHAN79807    08/09/2017  White  $3600  $3650   
3     45973          9889      YHAN79807    08/09/2017  White  $3600  $3650   

     csv_date      new  
0  01/31/2017      NaN  
1  01/31/2017      NaN  
2  01/31/2017      NaN  
3  03/31/2017  succeed  

【讨论】:

  • 非常感谢@jezrael,这就像一个魅力。我有第一个在我的代码中工作,但这不是我想要的。第二个解决方案是我正在寻找的。它就像一个魅力。
猜你喜欢
  • 2019-12-18
  • 1970-01-01
  • 2021-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-21
  • 2013-02-03
  • 2020-10-07
相关资源
最近更新 更多