假设您有以下数据框:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.nan,columns=['A','B','C'],index=[0,1,2])
假设我想要在这个 pandas 数据框之上添加一个“层”,这样 A 列第 0 行会有它的值,B 列第 0 行会有不同的值,C 列第 0 行会有一些东西,column A排1等等。所以就像在这个现有数据框架之上的数据框架。
是否可以添加其他图层?如何访问这些层?这是否有效,即我应该一起使用一个单独的数据框吗?是否可以通过访问各个层将这些多层保存为 csv?或者是否有一种功能可以将它们分解为同一工作簿中的不同工作表?
最佳答案
pandas.DataFrame
cannot have 3 dimensions :
DataFrame
is a 2-dimensional labeled data structure with columns of potentially different types.
但是,有一种方法可以用 MultiIndex / Advanced Indexing 伪造 3 维:
Hierarchical indexing (MultiIndex)
Hierarchical / Multi-level indexing is very exciting as it opens the door to some quite sophisticated data analysis and manipulation, especially for working with higher dimensional data. In essence, it enables you to store and manipulate data with an arbitrary number of dimensions in lower dimensional data structures like Series (1d) and DataFrame (2d).
如果您真的需要额外的维度,请使用 pandas.Panel
:
Panel
is a somewhat less-used, but still important container for 3-dimensional data.
但不要错过文档中的重要免责声明:
Note: Unfortunately
Panel
, being less commonly used thanSeries
andDataFrame
, has been slightly neglected feature-wise. A number of methods and options available inDataFrame
are not available inPanel
.
还有pandas.Panel4D
(experimental)在不太可能需要它的情况下。
关于python - Pandas 数据框有一个额外的 "layer",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42632611/