DataFrame赋值报错A value is trying to be set on a copy of a slice from a DataFrame.

这几天刚学习Pandas,买了网课拿数据进行实战练习

建了一个DataFrame,准备增加一列新数据,赋值代码虽然正常运行了,但出现以下报错

In:
seasons = season(data2['月'])
reduced_features = data2[['前一天风速', '前一天最高温度', '每年该日最高温度均值', '当天最高温度']]
reduced_features['季节'] = seasons

Out:
<ipython-input-73-dd2221e85f95>:3: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  reduced_features['季节'] = seasons

于是我改了一下,结果还是报错,报错还更长了……去

In:
#更改后,依然报错
reduced_features.loc[:,'季节'] = seasons

去看了一下报错里给的官网链接……还是没看懂。

最后新通过建变量来赋值,终于……终于不报错了!!!!(代码如下)

In:
#增加了newc、newd两个变量
#newc是新建的标签
#newd是新建的Datarame,作为一个中介变量,把赋值后的变量传递给原来的变量
#season()是一个我自己定义的函数,返回的是一个列表
seasons = season(data2['月'])
reduced_features = data2[['前一天风速', '前一天最高温度', '每年该日最高温度均值', '当天最高温度']]
newc = reduced_features.columns.insert(len(reduced_features.columns), '季节')
newd = reduced_features.reindex(columns = newc, fill_value = seasons)
reduced_features = newd

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值