问题
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