以下是一些标题选项,每个标题选项都强调解决方案的不同方面: 聚焦问题: * 如何在没有内存错误的情况下处理大型 Pandas DataFrame? * Pandas 中的内存错误:

Patricia Arquette
发布: 2024-10-27 06:19:29
原创
828 人浏览过

Here are a few title options, each highlighting a different aspect of the solution:

Focusing on the Problem:

* How to Process Large Pandas DataFrames Without Memory Errors?
* Memory Error in Pandas:  Efficiently Handling Large Dataframes?

Focusing on t

对大型 Pandas 数据帧进行切片

问题:

尝试通过函数导致内存错误,表明数据帧大小过大。目标是:

  1. 将数据帧分成更小的段。
  2. 迭代函数内的更小的块。
  3. 将处理后的段合并到单个数据帧中。

解决方案:

按行数切片

可以使用 list 来完成按固定行数的分割来自 numpy 的理解或 array_split:

<code class="python">n = 200000  # Chunk row size
list_df = [df[i:i + n] for i in range(0, df.shape[0], n)]</code>
登录后复制
<code class="python">list_df = np.array_split(df, math.ceil(len(df) / n))</code>
登录后复制

按 AcctName 切片

按特定列值进行切片,例如 AcctName:

<code class="python">list_df = []

for n, g in df.groupby('AcctName'):
    list_df.append(g)</code>
登录后复制

合并

大数据帧被切片后,可以使用 pd.concat 重新组装:

<code class="python">consolidated_df = pd.concat(list_df)</code>
登录后复制

以上是以下是一些标题选项,每个标题选项都强调解决方案的不同方面: 聚焦问题: * 如何在没有内存错误的情况下处理大型 Pandas DataFrame? * Pandas 中的内存错误:的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!