我正在获取SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
这是我的数据框(容量):
A B C D E
2020-01-01 00:00:00 4.0 66 15 3.8 3.2
2020-01-01 01:00:00 4.0 66 15 3.8 3.2
2020-01-01 02:00:00 4.0 66 15 3.8 3.2
.
.
.
2020-03-23 22:00:00 4.0 66 15 3.8 3.2
2020-03-23 23:00:00 4.0 66 15 3.8 3.2
我想根据日期更改属于A列的特定值。我的意思是,如果索引的月份为3且大于3,则将值15更改为20.40。
Capacity['A'][(Capacity.index.month >= 3)] = 20.40
如何编写此行以避免收到警告?
答案 0 :(得分:0)
使用DataFrame.loc
来设置DataFrame
,而不是Series
:
Capacity.loc[(Capacity.index.month >= 3), 'A'] = 20.40
更多信息,evaluation order matters
。