循环Pandas DF将新行附加到tx

2024-10-05 17:46:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我有平面文件(txt),其中有日期列和值列。在

我正在尝试将新行附加到txt,以防我的数据帧接收到新行,使用循环逻辑。我有以下代码: 为了简单起见,我在这里说它等于0。在

LastDate = 0
saveFileLine = name+'.txt.'
saveFile = open(saveFileLine,'a')

for index, row in namedf.iterrows():
    if int(''.join(row['Date'].split('-')[:3])) > LastDate:
        lineToWrite = row+'\n'
        saveFile.write(lineToWrite)

saveFile.close()

它给了我一个错误:

write()参数必须是str,而不是Series

我不知道如何使它写入当前活动的循环行。在

希望你能帮助我! 谢谢


Tags: 文件数据代码nametxt逻辑open平面
1条回答
网友
1楼 · 发布于 2024-10-05 17:46:40

每排都是熊猫系列。你把整排都当作一根绳子。你想写整行吗?在

for index, row in namedf.iterrows():
    lineToWrite = row.to_string()
    saveFile.write(lineToWrite)

相关问题 更多 >