如何将数据帧给定列的每个元素与标量相乘?
(我尝试过寻找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
EDIT:
我在跑步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
尝试使用应用功能。
df['quantity'] = df['quantity'].apply(lambda x: x*-1)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)