[英]Python Pandas SettingWithCopyWarning while creating new column
我有一個數據框 df 為:
df.iloc[1:5,1:3]
Date Month
4 2013-01-03 00:00:00 1
6 2013-01-04 00:00:00 1
10 2013-01-07 00:00:00 1
12 2013-01-08 00:00:00 1
我正在嘗試以下操作:
df['newCol'] = df['Month']*2
我收到以下警告:
<input>:1: 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
執行上述操作的正確方法是什么?
在這種情況下,按照您的方式分配值是安全的。 但是,如果你想避免警告以保持好習慣,你可以按照消息說的做,即:
df.loc[:, 'newCol'] = df['Month']*2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.