python - 如何使用 Pandas 访问列中的列

标签 python python-2.7 pandas

全部,

我有一个如下所示的数据框:df[['date','PRICE']]

df>>

date                   Price
                 PX_FIRST     PX_LAST

2018-03-05        1.710       -0.511
2018-03-06        1.725       -0.513
2018-03-07        1.745       -0.511
2018-03-08        1.750       -0.512

如何获得与此类似的数据框?换句话说,我如何访问 PX_FIRST 和 PX_LAST。当我执行 df[['date','PRICE']] 时,它无法访问各个列。

  date           PX_FIRST     PX_LAST

2018-03-05        1.710       -0.511
2018-03-06        1.725       -0.513
2018-03-07        1.745       -0.511
2018-03-08        1.750       -0.512

最佳答案

如果需要选择第一层Price值下的列:

df = df['Price']

或者使用DataFrame.xs :

df = df.xs('Price', axis=1)
print (df)
            PX_FIRST  PX_LAST
Date                         
2018-03-05     1.710   -0.511
2018-03-06     1.725   -0.513
2018-03-07     1.745   -0.511
2018-03-08     1.750   -0.512

如果需要删除 MultiIndex 的顶层:

df.columns = df.columns.droplevel(0)

但是如果更多列具有不同的第一级(PricePrice1)和第二级相同的值,请小心:

#create sample data
df = pd.concat([df['Price'], df['Price'] * 0.4], keys=('Price','Price1'), axis=1)
print (df)
              Price           Price1        
           PX_FIRST PX_LAST PX_FIRST PX_LAST
Date                                        
2018-03-05    1.710  -0.511    0.684 -0.2044
2018-03-06    1.725  -0.513    0.690 -0.2052
2018-03-07    1.745  -0.511    0.698 -0.2044
2018-03-08    1.750  -0.512    0.700 -0.2048

删除第一层:

df.columns = df.columns.droplevel(0)
print (df)
            PX_FIRST  PX_LAST  PX_FIRST  PX_LAST
Date                                            
2018-03-05     1.710   -0.511     0.684  -0.2044
2018-03-06     1.725   -0.513     0.690  -0.2052
2018-03-07     1.745   -0.511     0.698  -0.2044
2018-03-08     1.750   -0.512     0.700  -0.2048

如果选择列 PX_FIRST 它返回 DataFrame,因为重复的列名:

print (df['PX_FIRST'])
            PX_FIRST  PX_FIRST
Date                          
2018-03-05     1.710     0.684
2018-03-06     1.725     0.690
2018-03-07     1.745     0.698
2018-03-08     1.750     0.700

如果需要两级选择,使用元组:

print (df[('Price', 'PX_FIRST')])
Date
2018-03-05    1.710
2018-03-06    1.725
2018-03-07    1.745
2018-03-08    1.750
Name: (Price, PX_FIRST), dtype: float64

关于python - 如何使用 Pandas 访问列中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49656116/

相关文章:

python - 如何使用 Python 将 Tick by Tick 数据转换为 OHLC 烛台数据?

python - 如何在网络上提供 PDF 文件?

python - Django http 连接超时

python - Lambda、可调用类实例和作用域;为什么它在 Python 2.7 中不起作用?

Python 属性实例行为

python - 从相似度矩阵创建 NetworkX 图

python - Windows Azure 网站 python

python - argparse (python) 是否支持互斥的参数组?

python - 清除 sys.path 后 python 如何进行导入 - 导入优先级

python - Pandas 打印选项