我正在用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')
请有人帮我一把。谢谢
答案 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' )