首页 > 解决方案 > pandas SettingWithCopyWarning,不完全明白

问题描述

我已经阅读了多个 SO 问题和https://www.dataquest.io/blog/settingwithcopywarning/它告诉了如何解决它但是我仍然不完全理解这个概念。

所以我有一个看起来像这样的数据框:

    user_id distance(km)    duration
0   251.0   0.554358        '00:03:12'  
1   270.0   0.861835        '00:04:12'  
2   192.0   0.701571        '00:05:12'  
3   192.0   0.453691        '00:01:12'  
4   192.0   0.192449        '00:07:12'  

对于每个持续时间,我将其转换为秒,这是由

dd_df["duration"] = dd_df["duration"].apply(lambda row:row.total_seconds())

或者

dd_df["duration"] = dd_df["duration"].loc.apply(lambda row:row.total_seconds())

两者都给我们 SeetingwithCopyWarning。

根据提到的博客,当有更改分配时会出现此错误。我正在分配新的 df dd_df['duration'] 并使用 apply 因此更改分配,所以我尝试过

`dd_df.loc[dd_df["duration"].apply(lambda row: row.total_seconds())]`

给我错误:

Passing list-likes to .loc or [] with any missing label will raise
KeyError in the future, you can use .reindex() as an alternative.

df.loc[:] 不是也获取数据帧的副本吗?

提前致谢!

标签: pythonpandas

解决方案


推荐阅读