I have a dataframe df as:
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
I am trying the following:
df['newCol'] = df['Month']*2
I get the following warning:
<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
What is the correct way to do the above?
In this case, it is safe to assign the value the way you did. However, if you want to avoid the warning to keep the good habit, you can do what the message says, ie:
df.loc[:, 'newCol'] = df['Month']*2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.