博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[pandas] SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
阅读量:7163 次
发布时间:2019-06-29

本文共 691 字,大约阅读时间需要 2 分钟。

转载自

问题场景:

      在读取CSV文件后,在新增一个特征列并根据已有特征修改新增列的值,结果在修改的过程中碰到SettingWithCopyWarning警告。

报错的做法

1 import pandas as pd  2 import numpy as np  3   4 aa = np.array([1,0,1,0])  5 bb = pd.DataFrame(aa.T, columns=['one'])  6   7 bb['two'] = 0  8   9 for i in range(bb.shape[0]): 10         bb['two'][i] = 1 11  12 print(bb)

正确的做法:

1 # -*- coding: utf-8 -*-  2 import pandas as pd  3 import numpy as np  4   5 aa = np.array([1,0,1,0])  6 bb = pd.DataFrame(aa.T, columns=['one'])  7   8 # 建立  9 two = np.zeros(bb.shape[0]) 10  11 # 修改 12 for i in range(bb.shape[0]): 13     two[i] = random.random() 14  15 # 插入 16 bb.insert(0,'two',two) 17  18 print(bb)

转载于:https://www.cnblogs.com/wanglei5205/p/8877835.html

你可能感兴趣的文章