Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我首先尝试从原始数据框中切出一些列,然后将附加列“索引”添加到最后一列。
df = df.iloc[:, np.r_[10:17]] #col 0~6 df['INDEX'] = df.index #col 7
我有第二行的错误消息说'试图在 DataFrame 的切片副本上设置一个值。尝试使用 .loc[row_indexer,col_indexer] = value 而不是' 为什么我会看到这个,我应该如何解决它?
我会做
df.loc[:,'INDEX'] = df.index
默认情况下,Python 对数据帧进行浅拷贝。因此,无论对数据帧执行什么操作,它实际上都会在原始数据帧上执行。消息正是表明了这一点。
下面的任何一个都会让 Python 解释器高兴:
df = df.iloc[:, np.r_[10:17]].copy()
或者
df.loc[:, ['INDEX']] = df.index