Value is tryin to be set on a copy of a slice from DF

风格不统一 提交于 2021-01-29 02:42:05

问题


I´m doing some stuff with pandas and python. I have the next code

df = pd.read_csv("Request.csv", keep_default_na=False)
df1 = df.loc[(df["Request Status"] == "Closed")]
df1["Request Close-Down Actual"] = pd.to_datetime(df1["Request Close-Down Actual"], errors = 'coerce' )
df3 = df1.loc[(df1["Request Close-Down Actual"] < '2016-11-01') | (df1["Request Close-Down Actual"].isnull())]        
df3.set_index("Request ID", inplace = True)
df3.to_csv("Request1.csv")

The issue is when i run the code i receive the next issue

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

df1.loc["Request Close-Down Actual"] = pd.to_datetime(df1["Request Close-Down Actual"], errors = 'coerce' )

Can someone give me a hand with this please. Thanks


回答1:


I test it and for me it works nice.

Problem should be in row above:

df1 = df.loc[(df["Request Status"] == "Closed")]

And solution is copy:

#loc is not necessary
df1 = df[df["Request Status"] == "Closed"].copy()

Error show loc - try remove it if need select column:

df1.loc["Request Close-Down Actual"] = pd.to_datetime(df1["Request Close-Down Actual"], errors = 'coerce' )

to:

df1["Request Close-Down Actual"] = pd.to_datetime(df1["Request Close-Down Actual"], errors = 'coerce' )


来源:https://stackoverflow.com/questions/42027229/value-is-tryin-to-be-set-on-a-copy-of-a-slice-from-df

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!