如何解决“系列的真值不明确。在Python Pandas中使用a.empty、a.bool()、a.item()、a.any()或a.all()”?

sum*_*593 2 python dataframe pandas

我有一个数据集,其中有两个时间戳列,一个是开始时间,另一个是结束时间。我已经计算了差异并将其存储在数据集中的另一列中。根据数据集的差异列,我想在另一列中填写一个值。我使用 for 循环和 if else 进行相同的操作,但在执行时出现错误“系列的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或a.all()”出现

   Time_df = pd.read_excel('filepath')

   print(Time_df.head(20))

   for index, rows in Time_df.head().iterrows():
         if(Time_df["Total Time"] < 6.00 ):
             Time_df["Code"] = 1

   print(Time_df.head(20))  
Run Code Online (Sandbox Code Playgroud)

在 Total Downtime 中,只要遇到小于 6 的地方,就会在列代码中放入 1。但是,我收到问题中所述的错误。

ank*_*_91 5

尝试使用np.where()


df["Code"]= np.where(df["Total Time"]<6.00,1,df["Code"])
Run Code Online (Sandbox Code Playgroud)

解释

#np.where(condition, choice if condition is met, choice if condition is not met)
#returns an array explained above
Run Code Online (Sandbox Code Playgroud)