DataFrame添加列时,警告A value is trying to be set on a copy of a slice from a DataFrame.

时间:2024-03-23 15:28:05

直接在dataframe中添加列,警告: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

方法一:
DataFrame添加列时,警告A value is trying to be set on a copy of a slice from a DataFrame.
警告:
DataFrame添加列时,警告A value is trying to be set on a copy of a slice from a DataFrame.
方法二:
DataFrame添加列时,警告A value is trying to be set on a copy of a slice from a DataFrame.
警告:
DataFrame添加列时,警告A value is trying to be set on a copy of a slice from a DataFrame.

解决方法:
先将testData以dataframe格式存储在x中,然后对x进行操作!
DataFrame添加列时,警告A value is trying to be set on a copy of a slice from a DataFrame.
运行结果:
DataFrame添加列时,警告A value is trying to be set on a copy of a slice from a DataFrame.