gpt4 book ai didi

python - 在函数内追加 Pandas 中的数据框

转载 作者:太空宇宙 更新时间:2023-11-03 17:53:54 25 4
gpt4 key购买 nike

我正在使用此函数和 APScheduler 每小时向数据帧添加行。问题是每次运行时它都会覆盖以前的条目,因此它并没有真正“附加”任何内容。

def SMSx(frame):
SMS(frame)
frame = frame.append(SMS.SMSdf)
frame = frame[frame.a != "test"]
frame = DataFrame(frame, columns=['Time', 'Hour', 'Date', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'Total', 'Bucket'])
frame.to_excel('Sprint_Log.xlsx', 'Sheet 1', index=False)
SMSx.frame = frame

如果我使用完全相同的代码(如下)并手动运行它,它就可以正常工作。我不太确定这里发生了什么。 SMS.SMSdf 是来自 SMS 功能的数据帧。

SMS(frame)
frame = frame.append(SMS.SMSdf)
frame = frame[frame.a != "test"]
frame = DataFrame(frame, columns=['Time', 'Hour', 'Date', 'Day', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'Total', 'Bucket'])
frame.to_excel('Sprint_Log.xlsx', 'Sheet 1', index=False)
SMSx.frame = frame

感谢您的帮助!

有效的代码:

def SMSx(frame_temp, frame_perm):
SMS(frame_temp)
try:
frame_perm = DataFrame.from_csv('Sprint_Log.csv')
except:
pass
frame_perm = frame_perm.append(SMS.SMSdf)
frame_perm = frame_perm.drop_duplicates()
frame_perm = frame_perm[frame_perm.Ready != "test"]
frame_perm = DataFrame(frame_perm, columns=['Time', 'Hour', 'Date', 'Day', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'Total', 'Bucket'])
frame_perm.to_csv('Sprint_Log.csv')
SMSx.frame2 = frame_perm

最佳答案

我怀疑的问题是您没有返回更新后的frame变量。当您在函数作用域中分配给 SMSx 变量时,一旦函数退出,该变量就会丢失。但是,我不确定这是如何工作的,因为您没有首先定义 SMSx 变量(它是当前函数的名称,或者也是一个全局变量?)

def SMSx(frame):
SMS(frame)
frame = frame.append(SMS.SMSdf)
frame = frame[frame.a != "test"]
frame = DataFrame(frame, columns=['Time', 'Hour', 'Date', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'Total', 'Bucket'])
frame.to_excel('Sprint_Log.xlsx', 'Sheet 1', index=False)
return frame


while True:
frame = SMSx(frame) # The returned frame will be used on the next iteration

如果不查看其余代码,就很难了解您要做什么。

关于python - 在函数内追加 Pandas 中的数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28771365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com