python - matplotlib 美国国债 yield 曲线

标签 python pandas dataframe matplotlib finance

我目前正在尝试构建一个包含每日美国国债利率的数据框。如您所见,pandas 会自动对列进行格式化,以便它们按顺序排列,这显然是我不想要的。这是我的一些代码。我只需要做一个小例子来说明我遇到的问题。

import quandl
import matplotlib.pyplot as plt

One_Month = quandl.get('FRED/DGS1MO')

^^ 对所有费率重复

Yield_Curve = pd.DataFrame({'1m': One_Month['Value'], '3m': Three_Month['Value'], '1yr': One_Year['Value']})
Yield_Curve.loc['2017-06-22'].plot()
plt.show()

enter image description here

Yield_Curve.tail()


             1m      1yr     3m
Date            
2017-06-16  0.85    1.21    1.03
2017-06-19  0.85    1.22    1.02
2017-06-20  0.88    1.22    1.01
2017-06-21  0.85    1.22    0.99
2017-06-22  0.80    1.22    0.96

正如我所说,我只向数据框添加了三个利率,但显然两年期、三年期和五年期利率也会导致问题。

我做了一些搜索,看到了这篇文章: Plotting Treasury Yield Curve, how to overlay two yield curves using matplotlib

虽然使用上一篇文章中的代码显然有效,但我宁愿能够保留我当前的数据集(One_Month、Three_Month ....)来执行此操作,因为我也将它们用于其他分析。

问题:有没有办法锁定列顺序?

感谢您的帮助!

最佳答案

如果您要定义列顺序,可以使用 reindex_axis():

df = df.reindex_axis(labels=['1m', '3m', '1yr'], axis=1)

df
              1m    3m   1yr
Date                        
2017-06-16  0.85  1.03  1.21
2017-06-19  0.85  1.02  1.22
2017-06-20  0.88  1.01  1.22
2017-06-21  0.85  0.99  1.22
2017-06-22  0.80  0.96  1.22

关于python - matplotlib 美国国债 yield 曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44751510/

相关文章:

python - 在 Dash 中下载 csv 文件

python - 使用相对 URL 从 NodeJS/Express 实例执行 Python 脚本

pandas - 在 Pandas 数据框上使用 groupby 按财政年度分组

python - 添加对的缺失值

python - 根据列值选择用户 - pandas dataframe

python - python中opencv阈值的最小blob大小

python - 如何简化通过索引搜索列表项的过程?

python - 合并两个不完全匹配时间戳的 pandas 数据帧

python - 针对在单个列中发现的离群值删除数据集的整行

python - 执行 Pandas Groupby 和百分比增加/减少