我将输出保存到列表中,我想创建一个新列并在其中添加值
我可以使用anaconda成功地做到这一点,但是我现在正在尝试使用python和IDLE,它不起作用
attendance = []
sleep(5)
for x in df1['Login Time']:
s1 = str(x)
s2 = '12:00:00'
s3 = '3:15:00'
s4 = '02:00:00'
FMT = '%H:%M:%S'
tdelta = datetime.strptime(s2, FMT) - datetime.strptime(s1, FMT)
if x <= s4:
attendance.append('Aussi')
elif (str(tdelta)) < s3:
attendance.append('Present')
else:
attendance.append('Check')
df1['Attendance'] = attendance
sleep(5)
df1.to_csv(datetime.strftime(datetime.now() - timedelta(1), '%Y-%m-%d')+".csv",index=False)
我遇到了错误 试图在DataFrame的切片副本上设置一个值。 尝试改用.loc [row_indexer,col_indexer] =值
答案 0 :(得分:0)
问题出在df1['Attendance'] = attendance
尝试一下:
attendance = []
sleep(5)
for x in df1['Login Time']:
s1 = str(x)
s2 = '12:00:00'
s3 = '3:15:00'
s4 = '02:00:00'
FMT = '%H:%M:%S'
tdelta = datetime.strptime(s2, FMT) - datetime.strptime(s1, FMT)
if x <= s4:
attendance.append('Aussi')
elif (str(tdelta)) < s3:
attendance.append('Present')
else:
attendance.append('Check')
#From Here......................................................
attendance_df = pd.Dataframe(attendance,column=['Title']
df1.loc[attendance_df.index,'Login Time']=attendance_df['Title']
#...............................................................
sleep(5)
df1.to_csv(datetime.strftime(datetime.now() - timedelta(1), '%Y-%m-%d')+".csv",index=False)
希望这行得通。