1. 程式人生 > 其它 >python pandas Dataframe增加一列遇到A value is trying to be set on a copy of a slice from a DataFrame.

python pandas Dataframe增加一列遇到A value is trying to be set on a copy of a slice from a DataFrame.

技術標籤:pythonpython大資料pandasDataframe

df2是Dataframe資料,直接在其上面增加一列,使用如下程式碼:

df2.loc[:, 'new_date'] = df2['date'].apply(lambda x: x.split(' ')[0])

警告⚠️如下:

修改為如下兩種方法可正常使用:

x = df2['date'].apply(lambda x: x.split(' ')[0])
df2.loc[:, 'new_date_2'] = x