【问题标题】:Indexing and Data Columns in Pandas/PyTablesPandas/PyTables 中的索引和数据列
【发布时间】:2014-11-01 02:23:28
【问题描述】:

http://pandas.pydata.org/pandas-docs/stable/io.html#indexing

我对 Pandas HDF5 IO 中数据列的概念感到非常困惑。另外,在谷歌上搜索它也几乎找不到关于它的信息。由于我在一个涉及 HDF5 存储的大型项目中深入研究 Pandas,因此我想明确这些概念。

文档说:

您可以指定(和索引)您希望能够使用的某些列 执行查询(除了可索引的列,您可以 总是查询)。例如说你想执行这个常见的 操作,在磁盘上,并仅返回与此查询匹配的帧。 您可以指定 data_columns = True 强制所有列 数据列

这令人困惑:

  1. other than the indexable columns, which you can always query:什么是“可索引”列?不是所有列都“可索引”吗?这个词是什么意思?

  2. For instance say you want to perform this common operation, on-disk, and return just the frame that matches this query. 这与在 Pytable 上的正常查询有何不同;有没有data_columns的任何索引?

  3. 非索引、索引和 data_column 列之间的根本区别是什么?

【问题讨论】:

  • 我在使用 HDFStore.select_column 函数时遇到了同样的问题。只有在确定需要在data_columns 中设置该列后才发现这一点。 github 上的这个问题进一步深入探讨了这一点:github.com/pandas-dev/pandas/issues/21188

标签: python pandas pytables


【解决方案1】:

你应该试试看。

In [22]: df = DataFrame(np.random.randn(5,2),columns=['A','B'])

In [23]: store = pd.HDFStore('test.h5',mode='w')

In [24]: store.append('df_only_indexables',df)

In [25]: store.append('df_with_data_columns',df,data_columns=True)

In [26]: store.append('df_no_index',df,data_columns=True,index=False)

In [27]: store
Out[27]: 
<class 'pandas.io.pytables.HDFStore'>
File path: test.h5
/df_no_index                     frame_table  (typ->appendable,nrows->5,ncols->2,indexers->[index],dc->[A,B])
/df_only_indexables              frame_table  (typ->appendable,nrows->5,ncols->2,indexers->[index])          
/df_with_data_columns            frame_table  (typ->appendable,nrows->5,ncols->2,indexers->[index],dc->[A,B])

In [28]: store.close()
  • 您会自动获取存储帧的索引作为可查询列。默认情况下,不能查询其他列。

  • 如果您指定data_columns=Truedata_columns=list_of_columns,则这些将单独存储,然后可以随后查询。

  • 如果您指定index=False,则不会为可查询列自动创建PyTables 索引(例如index 和/或data_columns)。

要查看正在创建的实际索引(PyTables 索引),请参阅下面的输出。 colindexes 定义哪些列创建了实际的 PyTables 索引。 (我已经把它截断了一些)。

/df_no_index/table (Table(5,)) ''
  description := {
  "index": Int64Col(shape=(), dflt=0, pos=0),
  "A": Float64Col(shape=(), dflt=0.0, pos=1),
  "B": Float64Col(shape=(), dflt=0.0, pos=2)}
  byteorder := 'little'
  chunkshape := (2730,)
  /df_no_index/table._v_attrs (AttributeSet), 15 attributes:
   [A_dtype := 'float64',
    A_kind := ['A'],
    B_dtype := 'float64',
    B_kind := ['B'],
    CLASS := 'TABLE',
    FIELD_0_FILL := 0,
    FIELD_0_NAME := 'index',
    FIELD_1_FILL := 0.0,
    FIELD_1_NAME := 'A',
    FIELD_2_FILL := 0.0,
    FIELD_2_NAME := 'B',
    NROWS := 5,
    TITLE := '',
    VERSION := '2.7',
    index_kind := 'integer']
/df_only_indexables/table (Table(5,)) ''
  description := {
  "index": Int64Col(shape=(), dflt=0, pos=0),
  "values_block_0": Float64Col(shape=(2,), dflt=0.0, pos=1)}
  byteorder := 'little'
  chunkshape := (2730,)
  autoindex := True
  colindexes := {
    "index": Index(6, medium, shuffle, zlib(1)).is_csi=False}
  /df_only_indexables/table._v_attrs (AttributeSet), 11 attributes:
   [CLASS := 'TABLE',
    FIELD_0_FILL := 0,
    FIELD_0_NAME := 'index',
    FIELD_1_FILL := 0.0,
    FIELD_1_NAME := 'values_block_0',
    NROWS := 5,
    TITLE := '',
    VERSION := '2.7',
    index_kind := 'integer',
    values_block_0_dtype := 'float64',
    values_block_0_kind := ['A', 'B']]
/df_with_data_columns/table (Table(5,)) ''
  description := {
  "index": Int64Col(shape=(), dflt=0, pos=0),
  "A": Float64Col(shape=(), dflt=0.0, pos=1),
  "B": Float64Col(shape=(), dflt=0.0, pos=2)}
  byteorder := 'little'
  chunkshape := (2730,)
  autoindex := True
  colindexes := {
    "A": Index(6, medium, shuffle, zlib(1)).is_csi=False,
    "index": Index(6, medium, shuffle, zlib(1)).is_csi=False,
    "B": Index(6, medium, shuffle, zlib(1)).is_csi=False}
  /df_with_data_columns/table._v_attrs (AttributeSet), 15 attributes:
   [A_dtype := 'float64',
    A_kind := ['A'],
    B_dtype := 'float64',
    B_kind := ['B'],
    CLASS := 'TABLE',
    FIELD_0_FILL := 0,
    FIELD_0_NAME := 'index',
    FIELD_1_FILL := 0.0,
    FIELD_1_NAME := 'A',
    FIELD_2_FILL := 0.0,
    FIELD_2_NAME := 'B',
    NROWS := 5,
    TITLE := '',
    VERSION := '2.7',
    index_kind := 'integer']

因此,如果要查询列,请将其设为 data_column。如果不这样做,它们将按 dtype 存储在块中(更快/更少空间)。

您通常总是希望索引列以进行检索,但是,如果您正在创建多个文件然后将多个文件附加到单个存储中,您通常会关闭索引创建并在最后执行(因为这对于随手创建)。

请参阅the cookbook 了解一系列问题。

【讨论】:

  • 设置index=True有什么用?在我看来,我可以设置 data_columns=True,index=False,并且我仍然可以使用它的列查询表
  • 当然可以,但实际上您不会从索引导致任何查询的线性扫描中受益。 index=False 在追加时很有用,例如,乘法追加然后构造索引比追加索引更有效(对于大量数据)
  • 追加完成后如何构建索引?我在 pandas 文档中找不到示例?感谢您的帮助。
  • 如果你想添加到食谱中,那就是 gr8(做一个拉取请求)
猜你喜欢
  • 2017-10-17
  • 1970-01-01
  • 2018-09-16
  • 1970-01-01
  • 1970-01-01
  • 2016-10-16
  • 1970-01-01
  • 2019-01-03
  • 2022-06-29
相关资源
最近更新 更多