尝试在DF的切片副本上设置值

时间:2017-02-03 15:06:00

标签: python pandas dataframe copy

我正在用pandas和python做一些事情。我有下一个代码

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")

问题是我运行代码时收到下一期

  

正在尝试在DataFrame

的切片副本上设置值      

df1.loc [“请求关闭实际”] = pd.to_datetime(df1 [“请求   Close-Down Actual“],errors ='coerce')

请有人帮我一把。谢谢

1 个答案:

答案 0 :(得分:1)

我测试它,对我来说它很好用。

问题应排在上面:

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

解决方案是copy

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

错误显示loc - 如果需要选择列,请尝试将其删除:

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

为:

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