【问题标题】:Can I get the value of a index if there seems to be multiple indexes?如果似乎有多个索引,我可以获得索引的值吗?
【发布时间】:2021-07-22 21:52:57
【问题描述】:

我有这个 df:

ticker  GSPC.INDX   IBM.US  US10Y.INDX  adjustedRF  index_returns   equity_returns  excess_market_returns   alpha   beta
date                                    
2013-11-04  1764.180033 179.116667  2.634467    0.001043    0.001218    -0.002539   0.000175    -0.001419   0.647420
2013-11-05  1767.130033 179.103333  2.643033    0.001047    0.001670    -0.000074   0.000624    -0.001423   0.647479
2013-11-06  1760.203333 179.013333  2.642467    0.001047    -0.003941   -0.000500   -0.004988   -0.001427   0.647593
2013-11-07  1762.750000 179.726667  2.668500    0.001057    0.001441    0.003987    0.000385    -0.001428   0.647597
2013-11-08  1763.216667 180.956667  2.701867    0.001070    0.000263    0.006795    -0.000807   -0.001428   0.647452
... ... ... ... ... ... ... ... ... ...
2021-07-06  4351.336567 139.540000  1.368967    0.000542    0.002934    -0.016329   0.002392    -0.000276   0.391254
2021-07-07  4340.829900 139.780000  1.322733    0.000524    -0.002423   0.001710    -0.002947   -0.000276   0.391306
2021-07-08  4349.499833 140.693333  1.325433    0.000525    0.001990    0.006517    0.001465    -0.000275   0.391341
2021-07-09  4345.184800 141.130000  1.339500    0.000530    0.001308    0.006043    0.000782    -0.000276   0.391278
2021-07-10  4377.089850 141.220000  1.362000    0.000539    0.011215    0.005527    0.010676    -0.000271   0.391562
1992 rows × 9 columns

我不得不旋转这个表,所以我认为ticker & date 是索引(?)

df.columns 结果:

Index(['GSPC.INDX', 'IBM.US', 'US10Y.INDX', 'adjustedRF', 'index_returns',
       'equity_returns', 'excess_market_returns', 'alpha', 'beta'],
      dtype='object', name='ticker')

我想为这一年创建一个新列(我相信这可以解决问题 - df['year'] = pd.to_datetime(df['date'],format='%Y'))但它没有找到日期列,因为我认为它是某种索引?

如何访问它?

【问题讨论】:

标签: python pandas


【解决方案1】:

您的数据框可能会将日期存储在 DatetimeIndex 中。要访问数据框中每一行的年份,只需使用df.index.year

【讨论】:

    猜你喜欢
    • 2018-11-04
    • 2012-08-05
    • 2016-08-06
    • 2017-02-09
    • 2017-06-20
    • 1970-01-01
    • 2020-01-01
    • 2010-11-13
    • 1970-01-01
    相关资源
    最近更新 更多