python – Pandas pytable:如何指定MultiIndex元素的min_itemsize
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – Pandas pytable:如何指定MultiIndex元素的min_itemsize,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3339字,纯文字阅读大概需要5分钟。
内容图文
![python – Pandas pytable:如何指定MultiIndex元素的min_itemsize](/upload/InfoBanner/zyjiaocheng/707/10e05b7980b9439c9d418ddc3e9c7d58.jpg)
我将pandas数据帧存储为包含MultiIndex的pytable.
MultiIndex的第一级是对应于userID的字符串.现在,大多数userID长度为13个字符,但其中一些长度为15个字符.当我追加包含long userID的记录时,pytables会引发错误,因为它需要13个字符的字段.
ValueError('Trying to store a string with len [15] in [user] column but\nthis column has a limit of [13]!\nConsider using min_itemsize to preset the sizes on these columns',)
但是,我不知道如何为MultiIndex的元素设置属性min_itemsize.我试过{‘index’:15}并且它不起作用……
我知道我可以通过添加空格强制所有ID从头开始长达15个字符,但我希望尽可能避免这种情况.
谢谢您的帮助!
解决方法:
您需要指定要为其设置min_itemsize的多索引级别的名称.这是一个例子:
创建2个多索引帧
In [1]: df1 = DataFrame(np.random.randn(4,2),index=MultiIndex.from_product([['abcdefghijklm','foo'],[1,2]],names=['string','number']))
In [2]: df2 = DataFrame(np.random.randn(4,2),index=MultiIndex.from_product([['abcdefghijklmop','foo'],[1,2]],names=['string','number']))
In [3]: df1
Out[3]:
0 1
string number
abcdefghijklm 1 0.737976 0.840718
2 0.605763 1.797398
foo 1 1.589278 0.104186
2 0.029387 1.417195
[4 rows x 2 columns]
In [4]: df2
Out[4]:
0 1
string number
abcdefghijklmop 1 0.539507 -1.059085
2 1.263722 -1.773187
foo 1 1.625073 0.078650
2 -0.030827 -1.691805
[4 rows x 2 columns]
创建一个商店
In [9]: store = pd.HDFStore('test.h5',mode='w')
In [10]: store.append('df1',df1)
这是计算长度的
In [12]: store.get_storer('df1').table
Out[12]:
/df1/table (Table(4,)) ''
description := {
"index": Int64Col(shape=(), dflt=0, pos=0),
"values_block_0": Float64Col(shape=(2,), dflt=0.0, pos=1),
"number": Int64Col(shape=(), dflt=0, pos=2),
"string": StringCol(itemsize=13, shape=(), dflt='', pos=3)}
byteorder := 'little'
chunkshape := (1456,)
autoindex := True
colindexes := {
"index": Index(6, medium, shuffle, zlib(1)).is_csi=False,
"number": Index(6, medium, shuffle, zlib(1)).is_csi=False,
"string": Index(6, medium, shuffle, zlib(1)).is_csi=False}
这是你现在得到的错误
In [13]: store.append('df1',df2)
ValueError: Trying to store a string with len [15] in [string] column but
this column has a limit of [13]!
Consider using min_itemsize to preset the sizes on these columns
使用级别名称指定min_itemsize
In [14]: store.append('df',df1,min_itemsize={ 'string' : 15 })
In [15]: store.get_storer('df').table
Out[15]:
/df/table (Table(4,)) ''
description := {
"index": Int64Col(shape=(), dflt=0, pos=0),
"values_block_0": Float64Col(shape=(2,), dflt=0.0, pos=1),
"number": Int64Col(shape=(), dflt=0, pos=2),
"string": StringCol(itemsize=15, shape=(), dflt='', pos=3)}
byteorder := 'little'
chunkshape := (1394,)
autoindex := True
colindexes := {
"index": Index(6, medium, shuffle, zlib(1)).is_csi=False,
"number": Index(6, medium, shuffle, zlib(1)).is_csi=False,
"string": Index(6, medium, shuffle, zlib(1)).is_csi=False}
附加
In [16]: store.append('df',df2)
In [19]: store.df
Out[19]:
0 1
string number
abcdefghijklm 1 0.737976 0.840718
2 0.605763 1.797398
foo 1 1.589278 0.104186
2 0.029387 1.417195
abcdefghijklmop 1 0.539507 -1.059085
2 1.263722 -1.773187
foo 1 1.625073 0.078650
2 -0.030827 -1.691805
[8 rows x 2 columns]
In [20]: store.close()
内容总结
以上是互联网集市为您收集整理的python – Pandas pytable:如何指定MultiIndex元素的min_itemsize全部内容,希望文章能够帮你解决python – Pandas pytable:如何指定MultiIndex元素的min_itemsize所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。