【发布时间】:2021-04-25 22:14:37
【问题描述】:
我有一个数据框 (=used_dataframe),其中包含重复项。我需要创建一个包含这些重复项索引的列表
为此,我使用了在这里找到的函数:
Find indices of duplicate rows in pandas DataFrame
def duplicates(x):
#dataframe = pd.read_csv(x)
#df = dataframe.iloc[: , 1:]
df = x
duplicateRowsDF = df[df.duplicated()]
df = df[df.duplicated(keep=False)]
tuppl = df.groupby(list(df)).apply(lambda x: tuple(x.index)).tolist() #this is the function!
n = 1 # N. . .
indicees = [x[n] for x in tuppl]
return indicees
duplicates(used_df)
我需要的下一个函数是一个,我从数据集中删除重复项,我这样做了:
x= tidy(mn)
indices = duplicates(tidy(mn))
used_df = x
used_df['indexcol'] = range(0, len(tidy(mn)))
dropped = used_df[~used_df['indexcol'].isin(indices)]
finito = dropped.drop(columns=['indexcol'])
return finito
handling_duplicate_entries(used_df)
它有效 - 但是当我想检查我的解决方案时(评估,所有重复项都已删除)
我通过duplicates(handling_duplicate_entries(used_df))执行的操作应该返回一个空数据框以显示没有重复,它返回错误'DataFrame' object has no attribute 'tolist'.
在上面链接的问题中,这也被添加为评论但没有解决 - 坦率地说,我很想为重复功能找到不同的解决方案,因为我不太了解它,但到目前为止我还没有不。
【问题讨论】: