先从原dataframe取出一个子dataframe,然后再对其中的元素赋值,例如

s = d[d['col_1'] == 0]
s.loc[:, 'col_2'] = 1

就会出现报错:
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
解决方法:

  1. 使用推荐的 .loc[row_indexer,col_indexer] = value
  2. 如果不知道,就先copy,再赋值。
s = d[d['col_1'] == 0].copy()
s.loc[:, 'col_2'] = 1

相关文章:

  • 2023-01-09
  • 2022-12-23
  • 2021-04-26
  • 2021-08-21
  • 2021-10-03
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-22
  • 2022-02-18
  • 2021-05-06
  • 2022-12-23
相关资源
相似解决方案