如何检查python字典是否严格增加以及持续多长时间?

问题描述 投票:0回答:1

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

[我使用了雅虎财务库和熊猫数据框来获取其整个历史年度的年度股息。

例如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'}]

是否有办法检查字典值是否严格增加以及从2019年起股息增加的年数?

如上所述,2017年的股息减少至0.88。到2019年,股息将增加3年。

或者如果它是列表或熊猫数据框会更好吗?

赞赏任何建议。谢谢!

python pandas yahoo-finance dictionary-comprehension
1个回答
0
投票

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

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
© www.soinside.com 2019 - 2024. All rights reserved.