Python:数据框在索引规则上添加一列

时间:2018-10-11 05:09:08

标签: python pandas dataframe

有一个如下所示的数据框,其索引为[date,assetName]

如何在10天内以开盘价添加新列?

日期索引列的数据类型为

Timestamp('2007-02-01 00:00:00 + 0000',tz ='UTC')

from datetime import datetime
import pandas as pd
datetime_object = datetime.strptime('Feb 1 2007', '%b %d %Y')

market_df   = pd.DataFrame({'date': [
                                    datetime.strptime('Feb 1 2007', '%b %d %Y')
                                    ,datetime.strptime('Feb 1 2007', '%b %d %Y')
                                    ,datetime.strptime('Feb 1 2007', '%b %d %Y') 
                                    ,datetime.strptime('Feb 11 2007', '%b %d %Y')
                                    ,datetime.strptime('Feb 11 2007', '%b %d %Y')
                                    ,datetime.strptime('Feb 11 2007', '%b %d %Y') 
                                    ],
                            'assetName': ['AAPL', 'GOOGL', 'AMZN','AAPL', 'GOOGL', 'AMZN'],
                            'open': [20, 30.9, 40, 22.3, 35, 45],
                            'close': [20.5, 30.6, 40.5,22.3,35,45],
                           })
market_df = market_df.set_index(['date','assetName'])  

如何使用新列“ price_in_10_days”获得此数据框

date    assetName   open    close   price_in_10_days
2007-02-01  AAPL    20.0    20.5    22.3
2007-02-01  GOOGL   30.9    30.6    35.0
2007-02-01  AMZN    40.0    40.5    45.0
2007-02-11  AAPL    22.3    22.3    NaN
2007-02-11  GOOGL   35.0    35.0    NaN
2007-02-11  AMZN    45.0    45.0    NaN

Data Frame Sample

1 个答案:

答案 0 :(得分:2)

我相信您需要在unstack之前创建DatetimeIndex,然后在10shift之前创建stack

market_df['price_in_10_days']  = market_df['open'].unstack().shift(-1, freq='10D').stack()
print (market_df)

                      open  close  price_in_10_days
date       assetName                               
2007-02-01 AAPL       20.0   20.5              22.3
           GOOGL      30.9   30.6              35.0
           AMZN       40.0   40.5              45.0
2007-02-11 AAPL       22.3   22.3               NaN
           GOOGL      35.0   35.0               NaN
           AMZN       45.0   45.0               NaN