相关疑难解决方法(0)

为什么我要在pandas中复制一个数据框

从父数据帧中选择子数据帧时,我注意到一些程序员使用该.copy()方法复制数据帧.

他们为什么要复制数据框?如果我不复制会怎么样?

python pandas chained-assignment

137
推荐指数
5
解决办法
11万
查看次数

Python Pandas 警告:尝试在 DataFrame 切片的副本上设置值

我有一个 Pandas DataFrame,我想用以下代码更改列的所有值:

df["Population"] = round(df["Population"]/1000000,1)
Run Code Online (Sandbox Code Playgroud)

我收到以下警告:

A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  return super().rename(
<ipython-input-6-59bf041bb022>: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 caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  df["Population"] = round(df["Population"]/1000000,1)
Run Code Online (Sandbox Code Playgroud)

正确的做法是什么并避免此类警告?

感谢您的帮助!

python warnings pandas

13
推荐指数
1
解决办法
3万
查看次数

标签 统计

pandas ×2

python ×2

chained-assignment ×1

warnings ×1