我有一個數據框,其中包含不同生產商的許多計算機,在不同年份發布,他們的銷售數字......現在,我的目標是找到 2015 年發布的所有以前幾年都不存在的新計算機。這意味著我必須控制計算機名稱是否在 2015 年之前的任何年份中列出,如果是,我想刪除 2015 年列表中的這些計算機名稱。此外,還有2016、2017年發布的電腦……不應該被忘記。我想要知道這些新電腦的數量。好吧,我有很多值,我不知道名稱是否重復,只是年份不同,但這是我的第一個想法。df_noduplicates=df[df.Year<2016](subset=['Name'], keep='first')df_Year2013 = df[df.Year==2015]print(df_Year2015.shape(0))但我只收到錯誤“DataFrame”對象在運行后不可調用。應該是因為第一行的原因,但我不知道,我做錯了什么。另一個問題是,我應該使用“set”來解決這個練習,但我不知道如何在這種情況下使用它。提前謝謝你的幫助。:)
1 回答

江戶川亂折騰
TA貢獻1851條經驗 獲得超5個贊
怎么樣 :
#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)
添加回答
舉報
0/150
提交
取消