简体   繁体   English

在drop_duplicates之后,Pandas DataFrame to_dict失败

[英]Pandas DataFrame to_dict fails after drop_duplicates

I have a DataFrame, let's just call it df . 我有一个DataFrame,我们就称它为df

return df.to_dict(orient="records") dutifully spits out a list of dicts. return df.to_dict(orient="records")忠实地吐出字典列表。

But if I do 但是如果我这样做

df.drop_duplicates
return df.to_dict(orient="records")

it fails and says: 它失败并说:

'function' object has no attribute 'to_dict' “功能”对象没有属性“ to_dict”

I think you miss () , because without the () the drop_duplicates just refers to the function, so df becomes a copy of the function, not the result of executing it (thanks andychase for comment): 我想您会错过() ,因为如果没有()drop_duplicates只会引用该函数,因此df成为该函数的副本,而不是执行该函数的结果(感谢andychase进行评论):

df = df.drop_duplicates()
return df.to_dict(orient="records")

Or: 要么:

df.drop_duplicates(inplace=True)
return df.to_dict(orient="records")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM