【问题标题】:Reduce Pandas Memory Footprint?减少 Pandas 内存占用?
【发布时间】:2026-01-24 07:30:01
【问题描述】:

刚刚启动了一个 Jupyter 终端并将一个 Excel 文件 (~12MB) 加载到 Pandas 数据帧中

加载文件之前:

>> import resource
>> print 'Memory usage: %s (Mb)' % (resource.getrusage(resource.RUSAGE_SELF).ru_maxrss/1024)

内存使用量:40 (Mb)

将文件加载到 Pandas 数据框后:

>> import pandas as pd
>> df = pd.read_excel('/var/www/temp_test_files/*_survey_2016.xlsx')
>> print 'Memory usage: %s (Mb)' % (resource.getrusage(resource.RUSAGE_SELF).ru_maxrss/1024)

内存使用量:193 (Mb)

为什么一个 12Mb 的文件在 pandas 中加载时占用的内存是其实际大小的 12 倍多 150mb?

下面列 dtypes 的详细分类。我猜对象 dtypes 分配的内存比列的实际使用量更多?

>> df.info(memory_usage=True)

<class 'pandas.core.frame.DataFrame'>
Int64Index: 56030 entries, 0 to 56029
Data columns (total 57 columns):
collector                   56030 non-null object
country                     55528 non-null object
un_subregion                55313 non-null object
so_region                   55390 non-null object
age_range                   55727 non-null object
age_midpoint                55336 non-null float64
gender                      55586 non-null object
self_identification         54202 non-null object
occupation                  49519 non-null object
occupation_group            46934 non-null object
experience_range            49520 non-null object
experience_midpoint         49520 non-null float64
salary_range                46121 non-null object
salary_midpoint             41742 non-null float64
programming_ability         46982 non-null float64
employment_status           49576 non-null object
industry                    40110 non-null object
company_size_range          39932 non-null object
team_size_range             39962 non-null object
women_on_team               39808 non-null object
remote                      40118 non-null object
job_satisfaction            40110 non-null object
job_discovery               40027 non-null object
commit_frequency            46598 non-null object
hobby                       46673 non-null object
dogs_vs_cats                45239 non-null object
desktop_os                  46451 non-null object
unit_testing                46657 non-null object
rep_range                   46143 non-null object
visit_frequency             46154 non-null object
why_learn_new_tech          46145 non-null object
education                   44955 non-null object
open_to_new_job             44380 non-null object
new_job_value               43658 non-null object
job_search_annoyance        42851 non-null object
interview_likelihood        42263 non-null object
star_wars_vs_star_trek      34398 non-null object
agree_tech                  42662 non-null object
agree_notice                42755 non-null object
agree_problemsolving        42659 non-null object
agree_diversity             42505 non-null object
agree_adblocker             42627 non-null object
agree_alcohol               42692 non-null object
agree_loveboss              42096 non-null object
agree_nightcode             42613 non-null object
agree_legacy                42382 non-null object
agree_mars                  42685 non-null object
important_variety           42628 non-null object
important_control           42572 non-null object
important_sameend           42531 non-null object
important_newtech           42604 non-null object
important_buildnew          42538 non-null object
important_buildexisting     42580 non-null object
important_promotion         42483 non-null object
important_companymission    42529 non-null object
important_wfh               42582 non-null object
important_ownoffice         42538 non-null object
dtypes: float64(4), object(53)
memory usage: 24.8+ MB
None

是否有任何“最佳实践”方法来减少 Pandas 数据帧的实际内存占用?

  • 修改数据类型?
  • 分类?

【问题讨论】:

标签: python pandas memory dataframe categorical-data


【解决方案1】:

为什么一个 12Mb 的文件在 pandas 中加载后,占用了 150mb 以上 是实际内存大小的 12 倍?

那个 12MB 的 Excel 文件是压缩格式的。文件中原始数据的实际大小可能是该文件的 5 或 10 倍。您可以验证是否只是将文件重命名为 .zip 并提取其内容。

是否有任何“最佳实践”方法来减少实际内存 熊猫数据框的足迹?修补 dtypes ?分类?

是的,使用dtypescategory 数据类型和向下转换是最好的方法。

由于您的大多数列都是字符串类型,因此减少内存的最佳选择是使用dtypes 为低基数列指定显式category 类型。 例如。国家、地区、性别、地位。

您的所有 4 个 float64 列也可以简化为无符号 32 位数据类型(或更低:如年龄、薪水)

【讨论】: