Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何获取索引项的内容?_Python_Pandas - Fatal编程技术网

Python 如何获取索引项的内容?

Python 如何获取索引项的内容?,python,pandas,Python,Pandas,我设置了一个数据框,除了我的数据外,还使用如下多索引存储相应的单元: Name Relative_Pressure Volume_STP Unit - ccm/g Description p/p0 0 0.042691 29.3601 1 0.078319 30.3071 2

我设置了一个数据框,除了我的数据外,还使用如下多索引存储相应的单元:

Name    Relative_Pressure         Volume_STP
Unit                    -              ccm/g
Description          p/p0   
0                0.042691            29.3601
1                0.078319            30.3071
2                0.129529            31.1643
3                0.183355            31.8513
4                0.233435            32.3972
5                0.280847            32.8724
                      Relative                                                  Volume @ STP
                      Pressure                                                         
                                                                                    cc/g

                         4.26910e-02                                                29.3601
                         7.83190e-02                                                30.3071
                         1.29529e-01                                                31.1643
                         1.83355e-01                                                31.8513
                         2.33435e-01                                                32.3972
                         2.80847e-01                                                32.8724
                         3.34769e-01                                                33.4049
                         3.79123e-01                                                33.8401
df.xs('Volume_STP', axis=1).columns.remove_unused_levels().get_level_values(0).tolist()[0]
例如,现在我可以通过以下方式仅提取
Volume\u STP
数据

Unit            ccm/g
Description 
0             29.3601
1             30.3071
2             31.1643
3             31.8513
4             32.3972
5             32.8724
使用
.values
我可以获得一个numpy数据数组。但是,如何获取存储单元?我不知道我需要做什么来接收存储的
ccm/g
字符串

编辑:添加了如何生成数据帧的示例

假设我有一个如下所示的字符串:

Name    Relative_Pressure         Volume_STP
Unit                    -              ccm/g
Description          p/p0   
0                0.042691            29.3601
1                0.078319            30.3071
2                0.129529            31.1643
3                0.183355            31.8513
4                0.233435            32.3972
5                0.280847            32.8724
                      Relative                                                  Volume @ STP
                      Pressure                                                         
                                                                                    cc/g

                         4.26910e-02                                                29.3601
                         7.83190e-02                                                30.3071
                         1.29529e-01                                                31.1643
                         1.83355e-01                                                31.8513
                         2.33435e-01                                                32.3972
                         2.80847e-01                                                32.8724
                         3.34769e-01                                                33.4049
                         3.79123e-01                                                33.8401
df.xs('Volume_STP', axis=1).columns.remove_unused_levels().get_level_values(0).tolist()[0]
然后我使用这个函数:

def read_result(contents, columns, units, descr):
    df = pd.read_csv(StringIO(contents), skiprows=4, delim_whitespace=True,index_col=False,header=None)
    df.drop(df.index[-1], inplace=True)
    index = pd.MultiIndex.from_arrays((columns,  units, descr))
    df.columns = index
    df.columns.names = ['Name','Unit','Description']
    df = df.apply(pd.to_numeric)
    return df
像这样

def isotherm(contents):
    columns = ['Relative_Pressure','Volume_STP']
    units = ['-','ccm/g']
    descr = ['p/p0','']
    df = read_result(contents, columns, units, descr)
    return df

要在我的问题开始时生成数据帧。

As
df
有一个多索引作为列,
df.Volume\u STP
仍然是一个数据帧。因此,您仍然可以访问其
属性,并且相关项将位于索引0处,因为数据帧仅包含1个系列

因此,您可以通过以下方式提取名称:

print(df.Volume_STP.columns[0])
应该给出:
('ccm/g','')


最后,您使用
.columns[0][0]
提取单元,并使用
.columns[0][1]
提取描述。访问多索引/列上的值的通用方法是使用数据框的
索引。获取\u level\u值
列。获取\u level\u值
函数


在您的示例中,尝试
df.columns.get_level_values(1)
访问多级列“Unit”的第二级。如果您已经选择了一列,比如“Volume_STP”,那么您已经删除了顶层,在这种情况下,您的单位将处于第0级。

您可以执行以下操作:

Name    Relative_Pressure         Volume_STP
Unit                    -              ccm/g
Description          p/p0   
0                0.042691            29.3601
1                0.078319            30.3071
2                0.129529            31.1643
3                0.183355            31.8513
4                0.233435            32.3972
5                0.280847            32.8724
                      Relative                                                  Volume @ STP
                      Pressure                                                         
                                                                                    cc/g

                         4.26910e-02                                                29.3601
                         7.83190e-02                                                30.3071
                         1.29529e-01                                                31.1643
                         1.83355e-01                                                31.8513
                         2.33435e-01                                                32.3972
                         2.80847e-01                                                32.8724
                         3.34769e-01                                                33.4049
                         3.79123e-01                                                33.8401
df.xs('Volume_STP', axis=1).columns.remove_unused_levels().get_level_values(0).tolist()[0]
输出:

'ccm/g'

使用
xs
从“Volume_STP”中切片数据帧,然后选择列并删除列标题中未使用的部分,然后获取该切片最顶层的值,即单位。转换为列表,选择第一个值。

我已将图像替换为文本片段。为了使其更简单,以便我能够真正理解您的数据帧结构,您是否可以添加编码以生成此数据帧?您有一个名为['Name'、'Unit'、'Description']的三级列多索引,以及数据帧“row”索引的默认范围索引?@ScottBoston I在问题的开头添加了访问数据帧所需的代码。