在 Pandas DataFrame 中存储 3 维数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46884648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Storing 3-dimensional data in pandas DataFrame
提问by Jiaye
I am new to Python and I'm trying to understand how to manipulate data with pandas DataFrames. I searched for similar questions but I don't see any satisfying my exact need. Please point me to the correct post if this is a duplicate.
我是 Python 新手,我正在尝试了解如何使用 Pandas DataFrames 操作数据。我搜索了类似的问题,但没有看到任何满足我的确切需求的问题。如果这是重复的,请指出正确的帖子。
So I have multiple DataFrames with the exact same shape, columns and index. How do I combine them with labels so I can easily access the data with any column/index/label?
所以我有多个具有完全相同的形状、列和索引的 DataFrame。我如何将它们与标签结合起来,以便我可以轻松访问带有任何列/索引/标签的数据?
E.g. after the setup below, how do I put df1 and df2 into one DataFrame and label them with the names 'df1' and 'df2', so I can access data in a way like df['A']['df1']['b'], and get number of rows of df?
例如,在下面的设置之后,我如何将 df1 和 df2 放入一个 DataFrame 并用名称“df1”和“df2”标记它们,以便我可以像 df['A']['df1'] 这样的方式访问数据['b'],并获取 df 的行数?
>>> import numpy as np
>>> import pandas as pd
>>> df1 = pd.DataFrame([[1, 2], [3, 4]], columns=['A', 'B'], index=['a', 'b'])
>>> df2 = pd.DataFrame([[5, 6], [7, 8]], columns=['A', 'B'], index=['a', 'b'])
>>> df1
A B
a 1 2
b 3 4
>>> df2
A B
a 5 6
b 7 8
回答by jezrael
I think MultiIndex DataFrame
is answer created by concat
:
我认为MultiIndex DataFrame
是由concat
以下人创建的答案:
df = pd.concat([df1, df2], keys=('df1','df2'))
print (df)
A B
df1 a 1 2
b 3 4
df2 a 5 6
b 7 8
Then for basic select is possible use xs
:
然后对于基本选择可以使用xs
:
print (df.xs('df1'))
A B
a 1 2
b 3 4
And for select index and columns together use slicers:
对于选择索引和列一起使用切片器:
idx = pd.IndexSlice
print (df.loc[idx['df1', 'b'], 'A'])
3
Another possible solution is use panels.
另一种可能的解决方案是使用面板。
But in newer versions of pandas is deprecated.
但是在较新版本的 pandas 中已弃用。