[英]Loop Pandas DF to append new lines to txt
I have flat file (txt), in which I have Date column and Value column. 我有平面文件(txt),其中有“日期”列和“值”列。
I am trying to append new lines to the txt in case my dataframe receives new lines, using a loop logic. 我试图使用循环逻辑将新行添加到txt,以防我的数据帧收到新行。 I have this following code: LastDate I am saying here its equal to 0 for simplicity reasons.
我有以下代码:LastDate我出于简单原因在这里说它等于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()
and it gives me the error: 它给了我错误:
write() argument must be str, not Series write()参数必须是str,而不是Series
I dont know how to make it write the line of the loop currently active. 我不知道如何使它写入当前处于活动状态的循环行。
Hope you can help me out! 希望你能帮帮我! Thanks
谢谢
Each row is a Pandas Series. 每行是一个熊猫系列。 You're treating the whole row as a string.
您正在将整行视为字符串。 Do you want to write the whole row?
您要写整行吗?
for index, row in namedf.iterrows():
lineToWrite = row.to_string()
saveFile.write(lineToWrite)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.