2

我有现有的代码,我发现使用 pandas 数据帧来存储其他数据帧很有用,这在 pandas 0.10.1 中运行良好,但不再可能使用 pandas 0.12.0 显示此类数据帧。然而,仍然可以在这些数据帧中存储和检索。这是一个错误还是我做错了什么?

说明性示例:

在 0.10.1 中工作:

>>> import pandas as pd
>>> pd.__version__
'0.10.1'
>>> df1 = pd.DataFrame({'a':[1, 2, 3], 'b':[3, 4, 5]})
>>> df2 = pd.DataFrame([df1, df1+10])
>>> df2[0][0]
   a  b
0  1  3
1  2  4
2  3  5
>>> df2[0][1]
    a   b
0  11  13
1  12  14
2  13  15
>>> df2
        0
0  [a, b]
1  [a, b]
>>> 

并且不能在 pandas 0.12.0 中工作:

>>> import pandas as pd
>>> pd.__version__
'0.12.0'
>>> df1 = pd.DataFrame({'a':[1, 2, 3], 'b':[3, 4, 5]})
>>> df2 = pd.DataFrame([df1, df1+10])
>>> df2[0][0]
   a  b
0  1  3
1  2  4
2  3  5
>>> df2[0][1]
    a   b
0  11  13
1  12  14
2  13  15
>>> df2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/data/Local/Envs/latest/lib/python2.7/site-packages/pandas/core/base.py", line 40, in __repr__
    return str(self)
  File "/home/data/Local/Envs/latest/lib/python2.7/site-packages/pandas/core/base.py", line 20, in __str__
    return self.__bytes__()
  File "/home/data/Local/Envs/latest/lib/python2.7/site-packages/pandas/core/base.py", line 32, in __bytes__
    return self.__unicode__().encode(encoding, 'replace')
  File "/home/data/Local/Envs/latest/lib/python2.7/site-packages/pandas/core/frame.py", line 665, in __unicode__
    fits_horizontal = self._repr_fits_horizontal_()
  File "/home/data/Local/Envs/latest/lib/python2.7/site-packages/pandas/core/frame.py", line 646, in _repr_fits_horizontal_
    d.to_string(buf=buf)
  File "/home/data/Local/Envs/latest/lib/python2.7/site-packages/pandas/core/frame.py", line 1556, in to_string
    formatter.to_string()
  File "/home/data/Local/Envs/latest/lib/python2.7/site-packages/pandas/core/format.py", line 294, in to_string
    strcols = self._to_str_columns()
  File "/home/data/Local/Envs/latest/lib/python2.7/site-packages/pandas/core/format.py", line 247, in _to_str_columns
    fmt_values = self._format_col(i)
  File "/home/data/Local/Envs/latest/lib/python2.7/site-packages/pandas/core/format.py", line 398, in _format_col
    space=self.col_space)
  File "/home/data/Local/Envs/latest/lib/python2.7/site-packages/pandas/core/format.py", line 1366, in format_array
    return fmt_obj.get_result()
  File "/home/data/Local/Envs/latest/lib/python2.7/site-packages/pandas/core/format.py", line 1382, in get_result
    fmt_values = self._format_strings()
  File "/home/data/Local/Envs/latest/lib/python2.7/site-packages/pandas/core/format.py", line 1418, in _format_strings
    fmt_values.append(' %s' % _format(v))
  File "/home/data/Local/Envs/latest/lib/python2.7/site-packages/pandas/core/format.py", line 1404, in _format
    return '%s' % formatter(x)
  File "/home/data/Local/Envs/latest/lib/python2.7/site-packages/pandas/core/format.py", line 1394, in <lambda>
    formatter = (lambda x: com.pprint_thing(x, escape_chars=('\t', '\r', '\n'))) \
  File "/home/data/Local/Envs/latest/lib/python2.7/site-packages/pandas/core/common.py", line 2059, in pprint_thing
    quote_strings=quote_strings)
  File "/home/data/Local/Envs/latest/lib/python2.7/site-packages/pandas/core/common.py", line 1963, in _pprint_seq
    r.append(pprint_thing(next(s), _nest_lvl + 1, **kwds))
StopIteration
4

2 回答 2

2

请注意,从 Pandas 0.25.0 开始,这已被弃用。

除了评论中提到的错误之外,Panel当您的直觉或模型引导您创建数据框的数据框时,您应该考虑使用。

pd.Panel({0:df1, 1:df1+10})
Out[2]: 
<class 'pandas.core.panel.Panel'>
Dimensions: 2 (items) x 3 (major_axis) x 2 (minor_axis)
Items axis: 0 to 1
Major_axis axis: 0 to 2
Minor_axis axis: a to b

然后,您可以在上面的代码中完成以下操作:

p[0]
Out[4]: 
   a  b
0  1  3
1  2  4
2  3  5

p[1]
Out[5]: 
    a   b
0  11  13
1  12  14
2  13  15

但如果需要,您还可以更轻松地访问其他维度操作:

p.minor_xs('a')
Out[6]: 
   0   1
0  1  11
1  2  12
2  3  13
于 2013-10-25T16:46:21.630 回答
0

我强调了布德的回答。但是,使用的替代方法panel是使用DataFrameMultiIndex 的法线。这在对象逻辑中不像面板那么干净,但由于panel有很多功能尚未(尚未?)实现,因此它是有价值的替代方案。

于 2014-05-22T12:32:15.270 回答