熊猫数据框:检查数据是否单调减少多长时间?

时间:2019-11-17 14:45:59

标签: python pandas yahoo-finance dictionary-comprehension

我是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'}]

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

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

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

赞赏任何建议。谢谢!

1 个答案:

答案 0 :(得分: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