如何将数据帧的给定列的每个元素与标量相乘? (我试图寻找对SO,但似乎无法找到合适的解决方案)Python:Pandas Dataframe如何将整列与标量相乘
做这样的事情:
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
使用'dtype'检查该列的类型。我无法复制那个错误,给予完整的回溯也是很好的。 – Leb
我已编辑添加完整的跟踪...也没有错误,它的警告(为了清晰) – labheshr
我认为这是由该行以外的东西造成的,或者该行导致警告升高从早期产生。你得到的东西与切片数据帧有关。 – Leb