【问题标题】:adjust pandas dollar column for inflation根据通货膨胀调整熊猫美元列
【发布时间】:2019-10-01 00:13:36
【问题描述】:

我有一个数据框 df,其中有一个列 inc。 inc 列包含过去几年的美元金额。我想创建一个名为 adj 的新列,其中包含所有已根据通货膨胀调整的过去美元金额。谁能建议如何做到这一点?假设平均通货膨胀率也可以,它不必完全准确。此外,我提供的示例输出的 adj 列中的示例编号完全是虚构的。非常感谢任何提示。

df:

      inc           date
0     50000.0       12/01/1992
1     39216.0       11/01/2005
2     65000.0       06/01/1970

输出:

      inc           date            adj
0     50000.0       12/01/1992  6000.0
1     39216.0       11/01/2005  4100.0
2     65000.0       06/01/1970  100000.0

【问题讨论】:

  • 这个问题太笼统了。
  • 使用来自CRSP 的月度 CPI(消费者价格指数)数据。然后,将每个加 1 以创建复合因子,并使用 cumprod 计算复合值。追溯计算它,以便您每个月都有一个折扣系数。然后,简单地使用df.lookuploc 使用索引匹配将复合因子带入您的数据框,并将inc 乘以复合因子

标签: python python-3.x pandas numpy quandl


【解决方案1】:

您需要从 quandl 获取消费者价格指数。纳斯达克在 2018 年收购了 quandl,所以这里是注册和获取 api 密钥的链接。 quandll signup

1- 配置quandl

pip install quandl

import quandl
QUANDL_KEY='Paste Your key here'
quandl.ApiConfig.api_key=QUANDL_KEY

2- 获取通货膨胀数据并与您的df合并

start='any date'
end='2021-12-22'

df_cpi = quandl.get(dataset='RATEINF/CPI_USA',start_date=start,end_date=end)
df_cpi.rename(columns={'Value':'cpi'}, inplace=True)
# left join, which is a type of join (same use case in sql) that
# returns all rows from the left table and the matched rows from the right table while leaving the unmatched rows empty.
# df is your data frame
df_merged = df.join(df_cpi, how='left')
df_merged

3- 将“simple_rtn”和“inflation_rate”列添加到df_merged

# in your df, your price column is "inc"
df_merged['simple_rtn']=df_merged['inc'].pct_change()
df_merged['inflation_rate']=df_merged.cpi.pct_change()

4- 将“real_rtn”添加到df_merged

df_merged['real_rtn']=(df_merged.simple_rtn+1)/(df_merged.inflation_rate+1)-1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 2020-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多