如何将数据框架中给定列的每个元素与标量相乘? (我已经尝试了 SO,但似乎找不到正确的解决方案)
比如:
df['quantity'] *= -1 # trying to multiply each row's quantity column with -1
给我一个警告:
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
注意: 如果可能的话,我不希望在数据框架上迭代并做类似的事情... ... 因为我认为对整个列进行任何标准的数学运算都可能需要写一个循环:
for idx, row in df.iterrows():
df.loc[idx, 'quantity'] *= -1
编辑:
我正在运行熊猫的 0.16.2
完全追踪:
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 the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self.obj[item] = s