dataframe使用set()查找2015年的所有新计算机



我有一个Dataframe,里面有许多不同生产商的电脑,在不同的年份发布,它们的销售数字。。。

现在,我的目标是找到2015年发布的所有新电脑,这些电脑在以前的任何一年都不存在。这意味着我必须控制这些计算机名是否在2015年之前的任何年份中列出,如果是,我想从我的2015年列表中删除这些计算机名。此外,还有20162017年发布的电脑,。。。这是不应该忘记的。我想知道这些新电脑的数量。

嗯,我有很多值,我不知道是否有重复的名字,只有不同的年份,但这是我的第一个想法。

df_noduplicates=df[df.Year<2016](subset=['Name'], keep='first')
df_Year2013 = df[df.Year==2015]
print(df_Year2015.shape(0))

但我只得到错误"DataFrame"对象不可调用运行后。应该是因为第一句话,但我不知道,我做错了什么。

另一个问题是,我应该使用"set"来解决这个练习,但我不知道如何在这种情况下使用它。

提前感谢您的帮助。:(

怎么样:

#find all computers' names present before 2015
s = set(df[df.Year<2015]['Name'])
# extract from the dataframe the lines where the name isn't already in s AND are there in 2015 (be carefull about those parenthesis)
subset_df = df[(df.Name.isin(s)==False) & (df.Year==2015)]
#print the names directly from the subset :
new_names  = subset_df['Name'].tolist()
print(new_names)

最新更新