首页 > 解决方案 > Pandas 数据框:检查数据是否单调递减多长时间?

问题描述

我是 python 新手,正在尝试将其应用于分析股票。

我使用 yahoo 金融库和 panda 数据框来获取股票在整个历史中的年度股息。

例如,COLB 从 2003 年到 2019 年开始支付股息。

COLB start: 2003 end: 2019 diff: 16
date formatted_date   amount
0   1052141400     2003-05-05  0.04762
1   1060090200     2003-08-05  0.04762
2   1067869800     2003-11-03  0.04762
3   1076337000     2004-02-09  0.04762
4   1084195800     2004-05-10  0.07000
..         ...            ...      ...
63  1541514600     2018-11-06  0.40000
64  1549377000     2019-02-05  0.42000
65  1557235800     2019-05-07  0.42000
66  1565098200     2019-08-06  0.28000
67  1572964200     2019-11-05  0.28000

然后我按年过滤数据框,并将每年的红利汇总到字典中。其2003年至2019年的年度股息(div):

[{'DIV 2003': '0.14286000000000001', 'DIV 2004': '0.25762', 'DIV 2005': '0.39', 'DIV 2006': '0.5700000000000001', 'DIV 2007': '0.66', 'DIV 2008': '0.5800000000000001', 'DIV 2009': '0.07', 'DIV 2010': '0.04', 'DIV 2011': '0.27', 'DIV 2012': '0.9799999999999999', 'DIV 2013': '0.41000000000000003', 'DIV 2014': '0.94', 'DIV 2015': '1.5199999999999998', 'DIV 2016': '1.5300000000000002', 'DIV 2017': '0.88', 'DIV 2018': '1.1400000000000001', 'DIV 2019': '1.4000000000000001'}]

然后我使用熊猫数据框按降序对列进行排序:

df5 = df5.sort_index(ascending=False,axis=1)

我知道如何检查数据框是否单调递减。但是我们怎么知道数据帧减少了多长时间呢?

感谢任何建议。谢谢!

标签: pythonpandasyahoo-financedictionary-comprehension

解决方案


据我了解,您想比较每年的值吗?

dividend = [{'DIV 2003': '0.14286000000000001', 'DIV 2004': '0.25762', 'DIV 2005': '0.39', 'DIV 2006': '0.5700000000000001', 'DIV 2007': '0.66', 'DIV 2008': '0.5800000000000001', 'DIV 2009': '0.07', 'DIV 2010': '0.04', 'DIV 2011': '0.27',
             'DIV 2012': '0.9799999999999999', 'DIV 2013': '0.41000000000000003', 'DIV 2014': '0.94', 'DIV 2015': '1.5199999999999998', 'DIV 2016': '1.5300000000000002', 'DIV 2017': '0.88', 'DIV 2018': '1.1400000000000001', 'DIV 2019': '1.4000000000000001'}]
for p in dividend:
    index = list(p)
    for x in range(len(index) -1):
        if p[index[x]] < p[index[x + 1]]:
            print("The value has increased!", index[x + 1])
        else:
            print("Decreased!", index[x + 1])

输出:

The value has increased! DIV 2004
The value has increased! DIV 2005
The value has increased! DIV 2006
The value has increased! DIV 2007
Decreased! DIV 2008
Decreased! DIV 2009
Decreased! DIV 2010
The value has increased! DIV 2011
The value has increased! DIV 2012
Decreased! DIV 2013
The value has increased! DIV 2014
The value has increased! DIV 2015
The value has increased! DIV 2016
Decreased! DIV 2017
The value has increased! DIV 2018
The value has increased! DIV 2019

推荐阅读