So I'm trying to insert a new row to a csv file using pandas. It adds the data, but it doesn't make it into a new row for example
This is the current file:
id,balance
00,100
But when I insert a new row, it becomes like this:
id,balance
00,10001,0
I have tried adding \n to the id but it becomes like this:
id,balance
00,100"
01",0
This is my current code:
member_id = '\n' + str(member.id)
df1 = pd.DataFrame({
'id': [member_id],
'balance': [0]
})
df1.to_csv('member_file.csv', mode = 'a', header = False, index = False)
How do I make it so it adds it to another line?
Add a newline at the end of the file (before you call to_csv
) with:
with open('member_file.csv', "a") as myfile:
myfile.write('\n')
use pd.DataFrame.append()
df1 = pd.DataFrame.append({
'id': [member_id],
'balance': [0]
})
df1.to_csv('member_file.csv', mode = 'a', header = False, index = False)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.