我的数据来自作为CSV文件导出到GCS bucket的BigQuery,如果文件大小相当大,BigQuery将自动将数据分割成若干块。考虑到时间序列,时间序列可能分散在不同的文件中。我有一个自定义函数,我想应用到每个TimeseriesID
以下是数据的一些限制:
TimeseriesID
和TimeID
排序TimeID
的开头并不总是0以下是说明问题的初始设置:
# Please take note this is just for simplicity. The actual goal is not to calculate mean for all group, but to apply a custom_func to each Timeseries ID
def custom_func(x):
return np.mean(x)
# Please take note this is just for simplicity. In actual, I read the file one by one since reading all the data is not possible
df1 = pd.DataFrame({"TimeseriesID":['A','A','A','B'],"TimeID":[0,1,2,4],"value":[10,20,5,30]})
df2 = pd.DataFrame({"TimeseriesID":['B','B','B','C'],"TimeID":[5,6,7,8],"value":[10,20,5,30]})
df3 = pd.DataFrame({"TimeseriesID":['C','D','D','D'],"TimeID":[9,1,2,3],"value":[10,20,5,30]})
如果我只需要concat
所有文件,这应该很简单,但问题是如果我concat
所有的数据帧,那么它将无法放入内存中
我想要的输出应该与此类似,但是没有concat
所有文件
pd.concat([df1,df2,df3],axis=0).groupby('TimeseriesID').agg({"value":simple_func})
我也知道vaex
和dask
,但我想暂时坚持使用简单的熊猫。
我也对解决方案持开放态度,包括修改BigQuery以更好地分割文件
op提出的使用concat处理数百万条记录的方法对于内存/其他资源来说是过度消耗
我已经用Google Colab Nootebooks测试了OP代码,这是一个糟糕的方法
输出将是:
正如您所看到的,“concat”需要时间来处理。由于记录很少,因此未察觉到这一点。 方法如下:
您可以使用以下代码检查逻辑,我使用json存储数据:
更新日期:2021年10月6日 我已经针对OPs需求调整了代码。此部分创建细化的文件
更新日期:2021年10月7日 我已经更新了代码以避免混淆。这部分处理细化的文件
输出将是:
总结:
在处理大型数据集时,您应该始终关注将要使用的数据,并将其保持在最低限度。仅使用可行值
由基本运算符或python本机库执行操作时,处理时间更快
相关问题 更多 >
编程相关推荐