读取大型 CSV 并将其拆分为较小的块

Reading Large CSV and splitting it into smaller chunks

我正在尝试使用 python 读取和分析一个大型 csv 文件 (11.5 GB)。然后使用 Power BI 围绕它创建一些视觉效果。但是每次我 运行 任何命令行甚至对 Power BI 中的数据帧进行更改时,每次更改之间大约需要 20-30 分钟。

其中一个列标题是 DeviceID。我想将大型 CSV 拆分为多个 csv 文件,以便每个文件都包含属于一个唯一 DeviceID 值的数据。

目前,单个 Full.csv 文件中的数据框如下所示:

DeviceID    AreaName     Longitude    Latitude
12311       Dubai        55.55431     25.45631
12311       Dubai        55.55432     25.45634
12311       Dubai        55.55433     25.45637
12311       Dubai        55.55431     25.45621
12309       Dubai        55.55427     25.45627
12309       Dubai        55.55436     25.45655
12412       Dubai        55.55441     25.45657
12412       Dubai        55.55442     25.45656

在 运行 代码之后,单个 Full.csv 文件应该生成 3 个 csv 文件:12311.csv12309.csv12412.csv,每个看起来都像这个:

DeviceID    AreaName     Longitude    Latitude
12311       Dubai        55.55431     25.45631
12311       Dubai        55.55432     25.45634
12311       Dubai        55.55433     25.45637
12311       Dubai        55.55431     25.45621

DeviceID    AreaName     Longitude    Latitude
12309       Dubai        55.55427     25.45627
12309       Dubai        55.55436     25.45655

DeviceID    AreaName     Longitude    Latitude
12412       Dubai        55.55441     25.45657
12412       Dubai        55.55442     25.45656

我读到 python 中处理大文件的最佳方法是使用 pandasql 模块。我可以使用 pandsql 实现上面描述的内容吗?

谢谢

One of the column heading is DeviceID. I would like to split the large CSV into multiple csv files so that each file will have data that belongs to the one unique DeviceID value.

我认为这不会加快您在 PowerBI 中的处理速度,您是在 PowerQuery 中还是在 PowerBI 中进行计算?

但无论如何,您可以为 DeviceID 创建一个唯一值列表:

df = pd.read_csv('Full.csv')
uniquelist = list(df['DeviceID'].unique())

然后根据这个列表拆分并保存成csv文件:

for i in uniquelist:
   i = df.loc[df['DeviceID'] == i]
   i.to_csv

首先,您可以分块读取它,还是需要整个数据帧?这将有很大帮助。

import pandas as pd

row_count = 1000
for chunk in pd.read_csv(filename, chunksize=row_count): 
    print(chunk.head()) # process it

您是否考虑过获取 CSV 并将其放入 SQL 数据库?会加快速度。您将能够索引列,通过 SQL 进行基本聚合,并使用简单的 pd.read_sql 将所需的子样本放入 Pandas 以进行更复杂的处理。您将能够使用 SQL 数据库更快地进行计算。第二,你有多少内存?

如果python它不是强制性的,您可以使用 Miller (https://github.com/johnkerl/miller)。

开始
DeviceID,AreaName,Longitude,Latitude
12311,Dubai,55.55431,25.45631
12311,Dubai,55.55432,25.45634
12311,Dubai,55.55433,25.45637
12311,Dubai,55.55431,25.45621
12309,Dubai,55.55427,25.45627
12309,Dubai,55.55436,25.45655
12412,Dubai,55.55441,25.45657
12412,Dubai,55.55442,25.45656

和运行

mlr --csv --from input.csv put -q 'tee > $DeviceID.".csv", $*'

您将拥有这 3 个文件

#12311.csv
DeviceID,AreaName,Longitude,Latitude
12311,Dubai,55.55431,25.45631
12311,Dubai,55.55432,25.45634
12311,Dubai,55.55433,25.45637
12311,Dubai,55.55431,25.45621

#12412.csv
DeviceID,AreaName,Longitude,Latitude
12412,Dubai,55.55441,25.45657
12412,Dubai,55.55442,25.45656

#12309.csv
DeviceID,AreaName,Longitude,Latitude
12309,Dubai,55.55427,25.45627
12309,Dubai,55.55436,25.45655
<pre><code>
import pandas as pd
full=pd.read_csv('path of the file')
f12311=full[full['DeviceID']==12311]
f12309=full[full['DeviceID']==12309]
f12412=full[full['DeviceID']==12412]
f12311.to_excel('path where to save the file')
f12309.to_excel('path where to save the file')
f12412.to_excel('path where to save the file')
</code></pre>

注意:只需确保 'DeviceID' 列的 dtype 是 'int64' 如果它不是 int,您可以使用代码进行转换:

<pre><code>
full['DeviceID']=full['DeviceID'].astype('int64')
</code></pre>