gpt4 book ai didi

python - Pandas drop_duplicates - TypeError : type object argument after * must be a sequence, 未映射

转载 作者:太空狗 更新时间:2023-10-29 17:40:41 25 4
gpt4 key购买 nike

我更新了我的问题以提供更清晰的示例。

是否可以使用 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

最佳答案

我认为这是因为列表类型不可散列,这打乱了重复的逻辑。作为一种解决方法,您可以像这样转换为元组:

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

关于python - Pandas drop_duplicates - TypeError : type object argument after * must be a sequence, 未映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37792999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com