我目前正在做一个项目,我必须加载一个更大的.csv文件(1/2百万行),并在每一行做一些错误处理。你知道吗
到目前为止,我正在将我的.csv文件加载到一个“dataFrame”变量中:
#Load the datafile into DataFrame
dataFrame = pd.read_csv(filename,header=None,
names=["year", "month", "day", "hour", "minute", "second", "zone1", "zone2", "zone3", "zone4"])
然后我遍历数据帧中的每一行并执行错误处理,例如:
#Check rows for corrupted measurements
for i in range(len(dataFrame)+1):
#Define the row
try:
row = np.array(dataFrame.iloc[i,:], dtype=object)
except IndexError:
continue
#If condition to check if there are corrupted measurements
if not -1 in row:
continue
#Check fmode, ignore upper- or lowercase
#foward fill
if fmode.lower() in fmodeStr[0]:
(Error handling)
elif fmode.lower() in fmodeStr[1]:
(Error handling)
elif fmode.lower() in fmodeStr[2]:
(Error handling)
其中fmode只是一个字符串,指定用户希望处理哪种错误。你知道吗
到目前为止,代码的行数还不错(1000-5000)。 但是当.csv文件有1/2百万行时,它需要很长时间才能通过。这是非常明显的,因为我在循环遍历一个1/2百万行文件的每一行。你知道吗
我想知道什么样的解决方案可以最有效地加载这种大小的csv文件,同时对单个行执行一些操作?你知道吗
到目前为止,我已经调查了: -生成一个生成器函数来加载一行.csv文件,处理它,并将其保存在numpy矩阵中
使用chunksize选项加载.csv文件并在结尾连接
矢量计算(但是,错误处理包括用损坏行之前或之后的有效行替换损坏行)
也许你可以把以上的结合起来?无论如何,谢谢你抽出时间:)
对于那些有兴趣/需要更多说明的人,下面是完整的代码:https://github.com/danmark2312/Project-Electricity/blob/test/functions/dataLoad.py
目前没有回答
相关问题 更多 >
编程相关推荐