我们都知道这个问题,当你运行时出现内存错误:pandas 数据框的最大大小 https://stackoverflow.com/questions/23569771/maximum-size-of-pandas-dataframe/44207661
我也尝试读4大csv-files
使用以下命令:
files = glob.glob("C:/.../rawdata/*.csv")
dfs = [pd.read_csv(f, sep="\t", encoding='unicode_escape') for f in files]
df = pd.concat(dfs,ignore_index=True)
我接受的唯一按摩是:
C:..\conda\conda\envs\DataLab\lib\site-packages\IPython\core\interactiveshell.py:3214:
DtypeWarning:列 (22,25,56,60,71,74) 具有混合类型。指定
导入时的 dtype 选项或设置 low_memory=False。如果(产量来自
self.run_code(代码,结果)):
这应该没问题。
我的总数据框的大小为:(6639037, 84)
在没有内存错误的情况下是否可以有任何数据大小限制?这意味着 python 会自动跳过一些行而不告诉我?我过去和另一个程序有过这个,我不认为Python那么懒,但你永远不知道。
进一步阅读:后来我保存的是sqlite-file
,但我也不认为这应该是一个问题:
conn = sqlite3.connect('C:/.../In.db')
df.to_sql(name='rawdata', con=conn, if_exists = 'replace', index=False)
conn.commit()
conn.close()
您可以将生成器表达式传递给 concat
dfs = (pd.read_csv(f, sep="\t", encoding='unicode_escape') for f in files)
这样你就可以避免在内存中创建那个疯狂的列表。这可能会缓解内存限制的问题。
此外,您可以制作一个特殊的生成器,其中包含某些列的向下转换。
说吧,像这样:
def downcaster(names):
for name in names:
x = pd.read_csv(name, sep="\t", encoding='unicode_escape')
x['some_column'] = x['some_column'].astype('category')
x['other_column'] = pd.to_numeric(x['other_column'], downcast='integer')
yield x
dc = downcaster(names)
df = pd.concat(dc, ...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)