python - 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[:] 不是也获取数据帧的副本吗?
提前致谢!
解决方案
推荐阅读
- php - Laravel 从数据库中获取 Slug
- sql-server - 是否有可能以及如何使用 Entity Framework Core 创建触发器
- angular - 在页面加载时传递日期时,我没有以正确的格式获取日期范围。我正在使用 ngx-daterangepicker-material 插件
- angular - 以对象为数据源的角材料表
- c# - 从 WebView 生成的 PDF 不包括所有内容
- python - 如何更新字典,以便如果键“a”的值为“c”而不是“c”的键附加值“a”?
- shell - 如何从 Shell 脚本测试 Hive CRUD 查询
- tensorflow - 二进制交叉熵 Keras——它是如何聚合的?
- css - 使用 Lobotomized Owl 处理元素网格的边距
- sql-server - 当它是列的默认值时,无法在 SQL Server 中更改标量函数?