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

Python 读取大型CSV并将其拆分为较小的块,python,sql,pandas,csv,pandasql,Python,Sql,Pandas,Csv,Pandasql,我正在尝试使用python读取和分析一个大型csv文件(11.5GB)。然后使用Power BI在其周围创建一些视觉效果。但每次我运行任何命令行,甚至在PowerBI中更改数据帧,每次更改之间都需要20-30分钟 其中一个列标题是DeviceID。我想将大型CSV拆分为多个CSV文件,以便每个文件都有属于唯一DeviceID值的数据 <pre><code> import pandas as pd full=pd.read_csv('path of the file') f

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

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

<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>
当前,单个
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.csv
12309.csv
12412.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

我了解到在python中处理大型文件的最佳方法是使用pandasql模块。我是否能够使用pandsql实现上面描述的功能

谢谢

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

<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>
我认为这不会加快您在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\u SQL
将所需的子样本获取到Pandas中,以便进行更复杂的处理。使用SQL db,您将能够更快地计算。第二,您有多少RAM?

如果python不是强制性的,您可以使用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
注意:只需确保'DeviceID'列的数据类型为'int64' 如果不是int,则可以使用以下代码进行转换:


您的数据帧中总共有多少个唯一的
设备ID
?你的机器内存是多少?我不确定有多少唯一的
DeviceID
s。即使是一个简单的
df.head()
df.descripe
命令也需要很长时间才能运行!我机器的内存是16GB你的CSV的分隔符是什么??多空间??如果没有,请指定真正的分隔符。分隔符主要是逗号。csv文件实际上是一个由70个合并文件组成的文件。我必须这样做,以确保所有的
DeviceID
s都在一个数据帧中进行整理,以便以后可以对它们进行过滤。我忘了提到的一件事是,CSV文件中没有任何列标题。我试图运行这个命令来分配新的标题:
df=pd.read\u csv('out.csv',header=None,engine='python',error\u bad\u lines=False)df.columns=[“LicensePlate”,“ID1”,“CompanyName”,“Long”,“Lat”,“Data\u Time”,“Emirate”,“OriginArea”,“Road”]
我的机器内存是16GB
<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>
<pre><code>
full['DeviceID']=full['DeviceID'].astype('int64')
</code></pre>