数据帧上的python loc问题

我目前正在使用 jupyter notebook 开发一个关于 python 的项目。我想预测法国体育场的上座率(联赛 1)。

为了实现这一点,我用漂亮的汤从网上获取了数据。我现在正在尝试清理我的数据:我有一些体育场馆的缺失值,我想为特定球队(Olympique lyonnais)分配体育场馆。

我第一次尝试:

stats_match.stade[(stats_match.saison >= 2017) & (stats_match.domicile == 'Olympique Lyonnais') & (stats_match.stade.isna())] = 'Groupama stadium'

这给了我那个错误:

SettingWithCopyWarning:试图在 DataFrame 的切片副本上设置值

所以我按照说明进行了尝试:

stats_match.stade.loc((stats_match.saison >= 2017) & (stats_match.domicile == 'Olympique Lyonnais') & (stats_match.stade.isna())) = 'Groupama stadium'

这给了我:

文件“”,第 3 行 stats_match.stade = stats_match.stade.loc((stats_match.saison >= 2017) & (stats_match.domicile == 'Olympique Lyonnais') & (stats_match.domicile.isna())) = 'Groupama体育场'
语法错误:无法分配给函数调用

我在这里想念什么?我必须使用 .where 函数吗?非常感谢


一只萌萌小番薯
浏览 159回答 2
2回答

FFIVE

这应该是正确的;stats_match.loc[(stats_match.saison >= 2017) & (stats_match.domicile == 'Olympique Lyonnais') & (stats_match.stade.isna())] = 'Groupama stadium'

蓝山帝景

好吧,就像@roganjosh 说我把 () 而不是 [],但我现在有同样的警告“SettingWithCopyWarning,我已经收到了这个警告并使用了 .copy(),但是有没有更好的方法呢?
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python