将数据框附加到csv时Excel对齐列

时间:2020-03-27 23:27:47

标签: python pandas csv

我有一个程序,该程序以touchVelocities.insert(CGPoint(x: touchLocations[touchNumber].last!.x - touchLocations[touchNumber].first!.x, y: touchLocations[touchNumber].last!.y - touchLocations[touchNumber].first!.y), at: touchNumber) 作为输入,并根据我从csv中读取的HANDLE shmem; OBJECT_ATTRIBUTES attrs; UNICODE_STRING uniName; RtlInitUnicodeString(&uniName, L"\\BaseNamedObjects\\MyFileMappingObject"); InitializeObjectAttributes(&attrs, &uniName, OBJ_CASE_INSENSITIVE, NULL, NULL); if (!NT_SUCCESS(ZwOpenSection(&shmem, FILE_SHARE_READ, &attrs))) { log("Could not load shmem"); } else { log("shmem loaded successfully"); ZwClose(shmem); } 进行检查:

URL

如果df中不存在 Name ID Date URL 0 Faye 111 12/31/16 https://www.url1.com 1 Faye 111 3/31/17 https://www.url2.com 2 Mike 222 3/31/17 https://www.url3.com 3 Mike 222 6/30/18 https://www.url4.com 4 Mike 222 9/30/18 https://www.url5.com 5 Jim 333 9/30/18 https://www.url6.com ,程序将执行一些代码,然后将包含URL的新行添加到df中;否则,它将移至另一个URL

如果我运行,停止并重新启动程序,则该程序可以正常运行。但是,如果我直接从Excel中的csv文件中删除现有行(例如df)以重新处理该网址的数据,则会将其添加到列URL和第[1][4]行:

[5]

而不是在行df处添加新行,这是在纯文本编辑器(而不是Excel)中删除行时发生的情况:

   Name   ID      Date                    URL
0  Faye  111  12/31/16   https://www.url1.com
2  Mike  222   3/31/17   https://www.url3.com
3  Mike  222   6/30/18   https://www.url4.com
4  Mike  222   9/30/18   https://www.url5.com
5   Jim  333   9/30/18   https://www.url6.com    Faye  111   3/31/17   https://www.url2.com

我正在通过[6]将数据添加到现有的csv中,所以有人可以识别我在做什么错吗?

1 个答案:

答案 0 :(得分:0)

与其尝试将另一个(1行)数据帧作为CSV文件的另一个块附加:

df.to_csv('~/file.csv', mode='a', header=False, index=False)

首先将其附加到您的数据框

df = df.append(your_1_row_df, ignore_index=True)

然后将其写入您的~/file.csv,这次使用(默认)模式'w'(有效地重写):

df.to_csv('~/file.csv', header=False, index=False)