相关疑难解决方法(0)

Python:Pandas Dataframe如何将整个列与标量相乘

如何将数据帧的给定列的每个元素与标量相乘?(我试过看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 …
Run Code Online (Sandbox Code Playgroud)

python pandas chained-assignment

49
推荐指数
7
解决办法
10万
查看次数

标签 统计

chained-assignment ×1

pandas ×1

python ×1