我对熊猫和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])
但是,我只想将“dup\u index”列的这个值设置为merchandiseID中最后出现的“YHAN79807”。我不希望将“YHAN79807”的重复数据作为merchandiseID的其余行具有此值。它们应该是空白的。只有最后一个事件应该有这个ID。我还不能这样做。我试过几件事,一件是:
group = df1.groupby(['merchandiseID'])
df1_index = df1.set_index(['merchandiseID'])
df1[ (((len(group.indices[ind])-1)==group.indices[df1.merchandiseID])]['dup_index'] = 'succeed'
我尝试添加“success”作为第一步,以查看列比较是否会给出结果,但它给了我以下错误:
FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
结果=getattr(x,名称)(y)。。。 raise TypeError('无法将%s类型与序列进行比较'%
我束手无策。我错过了什么?如有任何提示,我们将不胜感激。你知道吗
最好的
爱丽丝
我想你需要:
或者,如果需要仅标识最后一个重复的行,请使用带
&
的双条件:相关问题 更多 >
编程相关推荐