pldcat 2021-11-25 06:41 采纳率: 50%
浏览 38
已结题

dataframe.loc赋值时产生告警

df1.loc[:,'data_index'] = df1.index

这个指令是哪里有问题?为什么总是有warning?

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
  self.obj[key] = _infer_fill_value(value)

loc我已经用了呀?看文档也没看明白。

  • 写回答

1条回答 默认 最新

  • python收藏家 2021-11-25 10:24
    关注
    
    df1.loc[:,'data_index'] = df1.index
    这个loc里对列取值的时候 一般 [] 括起来
    
    df1.loc[:,['data_index']] = df1.index
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 6月12日
  • 已采纳回答 6月4日
  • 创建了问题 11月25日