Python Pandas drop_duplicates-TypeError:在*之后的type对象参数必须是序列,而不是映射
我更新了我的问题,提供了一个更清晰的例子 是否可以使用Pandas中的drop_duplicates方法根据列id(其中值包含列表)删除重复行。考虑列“三”,它由列表中的两个项目组成。有没有一种方法可以删除重复的行,而不是以迭代的方式(这是我当前的解决方法) 我通过提供以下示例概述了我的问题:Python Pandas drop_duplicates-TypeError:在*之后的type对象参数必须是序列,而不是映射,python,pandas,dataframe,Python,Pandas,Dataframe,我更新了我的问题,提供了一个更清晰的例子 是否可以使用Pandas中的drop_duplicates方法根据列id(其中值包含列表)删除重复行。考虑列“三”,它由列表中的两个项目组成。有没有一种方法可以删除重复的行,而不是以迭代的方式(这是我当前的解决方法) 我通过提供以下示例概述了我的问题: import pandas as pd data = [ {'one': 50, 'two': '5:00', 'three': 'february'}, {'one': 25, 'two': '6:
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
我认为这是因为列表类型是不可散列的,这会弄乱重复的逻辑。作为一种解决方法,您可以像这样强制转换为tuple:
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
您想要
df\u two=df\u one.删除重复项('ID')
或者特别是df\u two=df\u one.删除重复项(子集=['ID'])
恐怕这还没有解决问题。仍然看到相同的错误df_two=df_one.drop_duplicates()
工作吗?不幸的是,没有,得到相同的错误您将不得不发布原始数据和代码来重现此错误,因为这似乎不是问题所在