lab*_*shr 49 python pandas chained-assignment
如何将数据帧的给定列的每个元素与标量相乘?(我试过看SO,但似乎找不到合适的解决方案)
做类似的事情:
df['quantity'] *= -1 # trying to multiply each row's quantity column with -1
Run Code Online (Sandbox Code Playgroud)
给我一个警告:
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
Run Code Online (Sandbox Code Playgroud)
注意:如果可能的话,我不想迭代数据帧并执行类似的操作...因为我认为整个列上的任何标准数学运算都应该可以不必编写循环:
for idx, row in df.iterrows():
df.loc[idx, 'quantity'] *= -1
Run Code Online (Sandbox Code Playgroud)
编辑:
我正在跑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
Run Code Online (Sandbox Code Playgroud)
mas*_*kar 47
尝试使用应用功能.
df['quantity'] = df['quantity'].apply(lambda x: x*-1)
Run Code Online (Sandbox Code Playgroud)
lab*_*shr 42
经过一些研究后,这是答案:
df.loc[:,'quantity'] *= -1 #seems to prevent SettingWithCopyWarning
Run Code Online (Sandbox Code Playgroud)
DJK*_*DJK 38
注意:对于使用pandas 0.20.3及更高版本的用户,并且正在寻找答案,所有这些选项都可以使用:
df = pd.DataFrame(np.ones((5,6)),columns=['one','two','three',
'four','five','six'])
df.one *=5
df.two = df.two*5
df.three = df.three.multiply(5)
df['four'] = df['four']*5
df.loc[:, 'five'] *=5
df.iloc[:, 5] = df.iloc[:, 5]*5
Run Code Online (Sandbox Code Playgroud)
结果
one two three four five six
0 5.0 5.0 5.0 5.0 5.0 5.0
1 5.0 5.0 5.0 5.0 5.0 5.0
2 5.0 5.0 5.0 5.0 5.0 5.0
3 5.0 5.0 5.0 5.0 5.0 5.0
4 5.0 5.0 5.0 5.0 5.0 5.0
Run Code Online (Sandbox Code Playgroud)
Sar*_*rah 13
为什么会出现错误的真正问题不在于您的代码有什么问题:您可以使用iloc, loc, or apply, or *=,其中另一个可以工作。
您遇到的真正问题在于您如何创建 df DataFrame。很可能您将 df 创建为另一个 DataFrame 的一个切片,而没有使用.copy(). 将 df 创建为另一个 DataFrame 的一个切片的正确方法是df = original_df.loc[some slicing].copy().
该问题已在您收到的错误消息中说明:“ SettingWithCopyWarning: 正在尝试在 DataFrame 的切片副本上设置值。尝试使用 .loc[row_indexer,col_indexer] = value 代替”
您将收到相同的消息在最新版本的熊猫中也是如此。
每当您收到此类错误消息时,您应该始终检查您创建 DataFrame 的方式。你可能忘记了.copy()
较新的熊猫版本具有pd.DataFrame.multiply函数。
df['quantity'] = df['quantity'].multiply(-1)
Run Code Online (Sandbox Code Playgroud)
有点旧,但我仍然得到相同的 SettingWithCopyWarning。这是我的解决方案:
df.loc[:, 'quantity'] = df['quantity'] * -1
Run Code Online (Sandbox Code Playgroud)