2016-06-13 37 views
8

我已更新我的問題以提供更清晰的示例。pandas drop_duplicates - TypeError:在*之後鍵入對象參數必須是一個序列,而不是映射

是否可以使用Pandas中的drop_duplicates方法根據值包含列表的列ID刪除重複的行。考慮由列表中的兩個項目組成的列「三」。有沒有辦法刪除重複的行,而不是迭代地做(這是我目前的解決方法)。

import pandas as pd 

data = [ 
{'one': 50, 'two': '5:00', 'three': 'february'}, 
{'one': 25, 'two': '6:00', 'three': ['february', 'january']}, 
{'one': 25, 'two': '6:00', 'three': ['february', 'january']}, 
{'one': 25, 'two': '6:00', 'three': ['february', 'january']}, 
{'one': 90, 'two': '9:00', 'three': 'january'} 
] 

df = pd.DataFrame(data) 

print(df) 

    one    three two 
0 50    february 5:00 
1 25 [february, january] 6:00 
2 25 [february, january] 6:00 
3 25 [february, january] 6:00 
4 90    january 9:00 

df.drop_duplicates(['three']) 

導致以下錯誤:

TypeError: type object argument after * must be a sequence, not map 
+1

'df_two = df_one.drop_duplicates( 'ID')'或'具體= df_two df_one.drop_duplicates(子集= [ 'ID'])' – EdChum

+0

害怕沒有解決問題。仍然看到相同的錯誤 – user3939059

+0

'df_two = df_one.drop_duplicates()'工作嗎? – EdChum

回答

15

我認爲這是因爲列表類型不是可哈希而這搞亂

我已經通過提供下面的例子列出我的問題重複的邏輯。作爲一種變通方法,你可以轉換爲元組,如下所示:

df['four'] = df['three'].apply(lambda x : tuple(x) if type(x) is list else x) 
df.drop_duplicates('four') 

    one    three two     four 
0 50    february 5:00    february 
1 25 [february, january] 6:00 (february, january) 
4 90    january 9:00    january 
相關問題