2016-06-13 5 views
8

명확한 예를 제공하기 위해 제 질문을 업데이트했습니다.Pandas drop_duplicates - TypeError : 유형 객체 인수 뒤에 *는 시퀀스가 ​​아니어야합니다.

값이 목록을 포함하는 열 ID를 기반으로 중복 행을 제거하려면 Pandas에서 drop_duplicates 메서드를 사용할 수 있습니까? 목록에서 두 항목으로 구성된 열 '3'을 고려하십시오. 중복 행을 반복적으로 수행하는 것보다 삭제하는 방법이 있습니까 (이것이 현재 해결 방법입니다). 다음과 같은 오류에

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 
관련 문제