zoukankan      html  css  js  c++  java
  • python数据分析——数据筛选实例【茅台】

    import tushare as ts
    import pandas as pd
    from pandas import DataFrame,Series

    DataFrame

    - 索引:
        - df[col] df[[c1,c2]]:取列
        - df.loc[index] : 取行
        - df.loc[index,col] : 取元素
    - 切片:
        - df[a:b]:切行
        - df.loc[:,a:b]:切列
    - df运算:Series运算一致
    - df级联:拼接
    In [8]:
    df = pd.read_csv('maotai.csv',index_col='date',parse_dates=['date'])
    df.drop(labels='Unnamed: 0',axis=1,inplace=True)
    df
    
    Out[8]:
     openclosehighlowvolumecode
    date      
    2001-08-27 5.392 5.554 5.902 5.132 406318.00 600519
    2001-08-28 5.467 5.759 5.781 5.407 129647.79 600519
    2001-08-29 5.777 5.684 5.781 5.640 53252.75 600519
    2001-08-30 5.668 5.796 5.860 5.624 48013.06 600519
    2001-08-31 5.804 5.782 5.877 5.749 23231.48 600519
    2001-09-03 5.812 5.779 5.870 5.757 22112.09 600519
    2001-09-04 5.782 5.852 5.949 5.762 37006.77 600519
    2001-09-05 5.876 5.849 5.924 5.813 26066.95 600519
    2001-09-06 5.835 5.734 5.854 5.704 28997.03 600519
    2001-09-07 5.702 5.574 5.773 5.570 31552.25 600519
    2001-09-10 5.531 5.734 5.757 5.470 18878.89 600519
    2001-09-11 5.749 5.670 5.773 5.656 11390.81 600519
    2001-09-12 5.520 5.621 5.656 5.515 25045.19 600519
    2001-09-13 5.626 5.607 5.671 5.577 10986.68 600519
    2001-09-14 5.626 5.671 5.702 5.593 7672.83 600519
    2001-09-17 5.637 5.599 5.670 5.546 8983.97 600519
    2001-09-18 5.606 5.663 5.710 5.601 10773.26 600519
    2001-09-19 5.671 5.768 5.768 5.634 8650.53 600519
    2001-09-20 5.765 5.720 5.788 5.702 11173.35 600519
    2001-09-21 5.668 5.634 5.718 5.624 7879.72 600519
    2001-09-24 5.634 5.632 5.685 5.624 4068.60 600519
    2001-09-25 5.668 5.660 5.709 5.632 3488.45 600519
    2001-09-26 5.642 5.637 5.699 5.624 4956.26 600519
    2001-09-27 5.637 5.734 5.777 5.624 8778.04 600519
    2001-09-28 5.765 5.795 5.812 5.702 17088.47 600519
    2001-10-08 5.781 5.715 5.809 5.663 6552.17 600519
    2001-10-09 5.718 5.827 5.859 5.718 9558.52 600519
    2001-10-10 5.827 5.640 5.848 5.629 17548.69 600519
    2001-10-11 5.626 5.585 5.702 5.570 12306.84 600519
    2001-10-12 5.609 5.624 5.726 5.320 20010.70 600519
    ... ... ... ... ... ... ...
    2019-01-16 658.000 661.100 665.650 653.600 42083.00 600519
    2019-01-17 663.000 659.250 669.700 655.800 30284.00 600519
    2019-01-18 665.000 683.610 690.200 661.500 68609.00 600519
    2019-01-21 690.000 686.980 698.880 681.880 47051.00 600519
    2019-01-22 686.000 671.000 686.000 665.220 42122.00 600519
    2019-01-23 667.220 665.660 674.500 663.130 33624.00 600519
    2019-01-24 670.300 672.500 677.830 664.680 29155.00 600519
    2019-01-25 675.880 679.700 684.900 669.930 28986.00 600519
    2019-01-28 684.000 677.000 693.980 676.030 36102.00 600519
    2019-01-29 677.500 685.000 688.000 668.000 30342.00 600519
    2019-01-30 684.990 676.000 686.220 676.000 22699.00 600519
    2019-01-31 680.000 689.600 689.600 677.560 36696.00 600519
    2019-02-01 697.040 692.670 699.000 689.610 30520.00 600519
    2019-02-11 695.000 725.300 729.500 688.180 58140.00 600519
    2019-02-12 723.000 717.920 725.490 715.200 35091.00 600519
    2019-02-13 723.000 735.250 736.160 717.220 35677.00 600519
    2019-02-14 731.200 737.160 743.660 730.140 32258.00 600519
    2019-02-15 734.880 724.780 734.880 723.020 23237.00 600519
    2019-02-18 731.000 740.000 744.950 731.000 37745.00 600519
    2019-02-19 740.960 730.760 742.500 726.660 29501.00 600519
    2019-02-20 735.000 722.000 735.000 716.060 37882.00 600519
    2019-02-21 720.000 720.250 730.480 717.000 29850.00 600519
    2019-02-22 717.000 726.010 728.490 714.100 25908.00 600519
    2019-02-25 730.750 742.330 744.440 717.990 82622.00 600519
    2019-02-26 747.800 727.350 749.180 726.000 57964.00 600519
    2019-02-27 733.000 740.700 758.020 731.000 65942.00 600519
    2019-02-28 737.900 755.010 763.420 736.950 56986.00 600519
    2019-03-01 761.500 789.300 790.000 761.000 63840.00 600519
    2019-03-04 795.000 781.860 798.330 777.220 84585.00 600519
    2019-03-05 785.000 779.780 789.550 775.880 44830.00 600519

    4174 rows × 6 columns

    In [9]:
    #假如我从2010年1月1日开始,每月第一个交易日买入1手股票,每年最后一个交易日卖出所有股票,到今天为止,我的收益如何?
    price_last = df['open'][-1]
    df = df['2010':'2019'] #剔除首尾无用的数据
    #Pandas提供了resample函数用便捷的方式对时间序列进行重采样,根据时间粒度的变大或者变小分为降采样和升采样:
    df_monthly = df.resample("M").first()
    df_yearly = df.resample("Y").last()[:-1] #去除最后一年
    cost_money = 0
    hold = 0 #每年持有的股票
    for year in range(2010, 2020):
        
        cost_money -= df_monthly.loc[str(year)]['open'].sum()*100
        hold += len(df_monthly[str(year)]['open']) * 100
        if year != 2019:
            cost_money += df_yearly[str(year)]['open'][0] * hold
            hold = 0 #每年持有的股票
    cost_money += hold * price_last
    
    print(cost_money)
    
     
    310250.69999999984
    
    In [7]:
    Out[7]:
     openclosehighlowvolumecode
    date      
    2010-01-04 109.760 108.446 109.760 108.044 44304.88 600519
    2010-01-05 109.116 108.127 109.441 107.846 31513.18 600519
    2010-01-06 107.840 106.417 108.165 106.129 39889.03 600519
    2010-01-07 106.417 104.477 106.691 103.302 48825.55 600519
    2010-01-08 104.655 103.379 104.655 102.167 36702.09 600519
    2010-01-11 104.400 102.926 105.230 102.422 24461.03 600519
    2010-01-12 103.028 105.708 106.040 102.492 31063.40 600519
    2010-01-13 104.649 103.022 105.389 102.741 37924.44 600519
    2010-01-14 103.379 107.552 107.974 103.379 46454.64 600519
    2010-01-15 107.533 108.401 110.641 107.533 45938.50 600519
    2010-01-18 108.484 109.110 109.926 108.420 21461.53 600519
    2010-01-19 109.116 108.337 109.441 108.165 17818.91 600519
    2010-01-20 108.427 105.881 108.580 105.804 20972.95 600519
    2010-01-21 105.842 106.397 107.450 105.613 17257.48 600519
    2010-01-22 106.314 104.738 106.755 103.309 25432.94 600519
    2010-01-25 104.560 105.957 106.761 103.704 23239.15 600519
    2010-01-26 107.380 106.378 108.593 105.038 32889.16 600519
    2010-01-27 105.951 104.643 107.068 104.400 19316.57 600519
    2010-01-28 104.566 107.974 108.708 104.336 30267.52 600519
    2010-01-29 108.452 107.552 108.612 107.004 37172.82 600519
    2010-02-01 107.769 107.776 108.216 106.576 29655.94 600519
    2010-02-02 107.208 106.263 108.484 106.117 15493.53 600519
    2010-02-03 106.066 105.887 107.272 104.783 23034.65 600519
    2010-02-04 105.868 107.591 108.006 105.376 22475.33 600519
    2010-02-05 106.959 109.282 109.684 106.570 26234.30 600519
    2010-02-08 109.282 109.269 112.058 108.816 31496.10 600519
    2010-02-09 109.760 109.193 110.609 108.822 14151.24 600519
    2010-02-10 109.760 109.652 110.137 108.931 6398.14 600519
    2010-02-11 109.633 110.641 111.318 109.505 14945.05 600519
    2010-02-12 111.018 110.456 111.164 109.888 9346.40 600519
    ... ... ... ... ... ... ...
    2019-01-16 658.000 661.100 665.650 653.600 42083.00 600519
    2019-01-17 663.000 659.250 669.700 655.800 30284.00 600519
    2019-01-18 665.000 683.610 690.200 661.500 68609.00 600519
    2019-01-21 690.000 686.980 698.880 681.880 47051.00 600519
    2019-01-22 686.000 671.000 686.000 665.220 42122.00 600519
    2019-01-23 667.220 665.660 674.500 663.130 33624.00 600519
    2019-01-24 670.300 672.500 677.830 664.680 29155.00 600519
    2019-01-25 675.880 679.700 684.900 669.930 28986.00 600519
    2019-01-28 684.000 677.000 693.980 676.030 36102.00 600519
    2019-01-29 677.500 685.000 688.000 668.000 30342.00 600519
    2019-01-30 684.990 676.000 686.220 676.000 22699.00 600519
    2019-01-31 680.000 689.600 689.600 677.560 36696.00 600519
    2019-02-01 697.040 692.670 699.000 689.610 30520.00 600519
    2019-02-11 695.000 725.300 729.500 688.180 58140.00 600519
    2019-02-12 723.000 717.920 725.490 715.200 35091.00 600519
    2019-02-13 723.000 735.250 736.160 717.220 35677.00 600519
    2019-02-14 731.200 737.160 743.660 730.140 32258.00 600519
    2019-02-15 734.880 724.780 734.880 723.020 23237.00 600519
    2019-02-18 731.000 740.000 744.950 731.000 37745.00 600519
    2019-02-19 740.960 730.760 742.500 726.660 29501.00 600519
    2019-02-20 735.000 722.000 735.000 716.060 37882.00 600519
    2019-02-21 720.000 720.250 730.480 717.000 29850.00 600519
    2019-02-22 717.000 726.010 728.490 714.100 25908.00 600519
    2019-02-25 730.750 742.330 744.440 717.990 82622.00 600519
    2019-02-26 747.800 727.350 749.180 726.000 57964.00 600519
    2019-02-27 733.000 740.700 758.020 731.000 65942.00 600519
    2019-02-28 737.900 755.010 763.420 736.950 56986.00 600519
    2019-03-01 761.500 789.300 790.000 761.000 63840.00 600519
    2019-03-04 795.000 781.860 798.330 777.220 84585.00 600519
    2019-03-05 785.000 779.780 789.550 775.880 44830.00 600519

    2221 rows × 6 columns

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    import tushare as ts
    
     

    使用tushare包获取某股票的历史行情数据

    In [14]:
    df = ts.get_k_data("600519",start="1988-01-01")
    df.to_csv("600519.csv")
    df
    
    Out[14]:
     dateopenclosehighlowvolumecode
    0 2001-08-27 5.392 5.554 5.902 5.132 406318.00 600519
    1 2001-08-28 5.467 5.759 5.781 5.407 129647.79 600519
    2 2001-08-29 5.777 5.684 5.781 5.640 53252.75 600519
    3 2001-08-30 5.668 5.796 5.860 5.624 48013.06 600519
    4 2001-08-31 5.804 5.782 5.877 5.749 23231.48 600519
    5 2001-09-03 5.812 5.779 5.870 5.757 22112.09 600519
    6 2001-09-04 5.782 5.852 5.949 5.762 37006.77 600519
    7 2001-09-05 5.876 5.849 5.924 5.813 26066.95 600519
    8 2001-09-06 5.835 5.734 5.854 5.704 28997.03 600519
    9 2001-09-07 5.702 5.574 5.773 5.570 31552.25 600519
    10 2001-09-10 5.531 5.734 5.757 5.470 18878.89 600519
    11 2001-09-11 5.749 5.670 5.773 5.656 11390.81 600519
    12 2001-09-12 5.520 5.621 5.656 5.515 25045.19 600519
    13 2001-09-13 5.626 5.607 5.671 5.577 10986.68 600519
    14 2001-09-14 5.626 5.671 5.702 5.593 7672.83 600519
    15 2001-09-17 5.637 5.599 5.670 5.546 8983.97 600519
    16 2001-09-18 5.606 5.663 5.710 5.601 10773.26 600519
    17 2001-09-19 5.671 5.768 5.768 5.634 8650.53 600519
    18 2001-09-20 5.765 5.720 5.788 5.702 11173.35 600519
    19 2001-09-21 5.668 5.634 5.718 5.624 7879.72 600519
    20 2001-09-24 5.634 5.632 5.685 5.624 4068.60 600519
    21 2001-09-25 5.668 5.660 5.709 5.632 3488.45 600519
    22 2001-09-26 5.642 5.637 5.699 5.624 4956.26 600519
    23 2001-09-27 5.637 5.734 5.777 5.624 8778.04 600519
    24 2001-09-28 5.765 5.795 5.812 5.702 17088.47 600519
    25 2001-10-08 5.781 5.715 5.809 5.663 6552.17 600519
    26 2001-10-09 5.718 5.827 5.859 5.718 9558.52 600519
    27 2001-10-10 5.827 5.640 5.848 5.629 17548.69 600519
    28 2001-10-11 5.626 5.585 5.702 5.570 12306.84 600519
    29 2001-10-12 5.609 5.624 5.726 5.320 20010.70 600519
    ... ... ... ... ... ... ... ...
    4118 2018-12-07 595.600 587.000 598.860 584.700 26841.00 600519
    4119 2018-12-10 576.000 577.230 587.500 574.180 24640.00 600519
    4120 2018-12-11 577.000 582.990 586.550 577.000 16316.00 600519
    4121 2018-12-12 588.790 589.000 589.200 582.800 19921.00 600519
    4122 2018-12-13 590.000 601.750 606.880 589.600 40399.00 600519
    4123 2018-12-14 598.010 592.960 599.800 592.000 24554.00 600519
    4124 2018-12-17 592.570 590.520 595.970 585.010 24895.00 600519
    4125 2018-12-18 585.880 590.940 591.540 585.000 19222.00 600519
    4126 2018-12-19 589.000 577.800 590.990 576.030 25715.00 600519
    4127 2018-12-20 576.220 574.500 581.500 568.890 26940.00 600519
    4128 2018-12-21 571.000 567.410 573.450 562.210 30640.00 600519
    4129 2018-12-24 563.500 568.000 570.910 558.000 19109.00 600519
    4130 2018-12-25 562.220 565.790 567.500 553.610 26592.00 600519
    4131 2018-12-26 563.810 560.080 568.400 558.000 17866.00 600519
    4132 2018-12-27 570.000 563.000 571.850 562.220 29493.00 600519
    4133 2018-12-28 563.300 590.010 596.400 560.000 63678.00 600519
    4134 2019-01-02 609.980 598.980 612.000 595.010 62286.00 600519
    4135 2019-01-03 599.970 590.000 601.660 585.800 30977.00 600519
    4136 2019-01-04 587.280 602.000 607.670 582.020 37683.00 600519
    4137 2019-01-07 608.000 605.490 612.000 602.220 34750.00 600519
    4138 2019-01-08 605.500 604.790 612.000 600.280 28838.00 600519
    4139 2019-01-09 609.990 616.120 626.160 609.040 49892.00 600519
    4140 2019-01-10 617.130 618.770 624.880 610.250 29043.00 600519
    4141 2019-01-11 625.000 635.880 637.000 618.750 38884.00 600519
    4142 2019-01-14 633.000 624.600 635.890 621.550 25648.00 600519
    4143 2019-01-15 627.860 659.980 661.500 625.000 55764.00 600519
    4144 2019-01-16 658.000 661.100 665.650 653.600 42083.00 600519
    4145 2019-01-17 663.000 659.250 669.700 655.800 30284.00 600519
    4146 2019-01-18 665.000 683.610 690.200 661.500 68609.00 600519
    4147 2019-01-21 690.000 686.980 698.880 681.880 47051.00 600519

    4148 rows × 7 columns

    In [15]:
    df.head()
    
    Out[15]:
     dateopenclosehighlowvolumecode
    0 2001-08-27 5.392 5.554 5.902 5.132 406318.00 600519
    1 2001-08-28 5.467 5.759 5.781 5.407 129647.79 600519
    2 2001-08-29 5.777 5.684 5.781 5.640 53252.75 600519
    3 2001-08-30 5.668 5.796 5.860 5.624 48013.06 600519
    4 2001-08-31 5.804 5.782 5.877 5.749 23231.48 600519
    In [20]:
    df = pd.read_csv("600519.csv", index_col='date',parse_dates=['date'])[['open','close','high','low']]
    df.head()
    
    Out[20]:
     openclosehighlow
    date    
    2001-08-27 5.392 5.554 5.902 5.132
    2001-08-28 5.467 5.759 5.781 5.407
    2001-08-29 5.777 5.684 5.781 5.640
    2001-08-30 5.668 5.796 5.860 5.624
    2001-08-31 5.804 5.782 5.877 5.749
     

    输出该股票所有收盘比开盘上涨3%以上的日期

    In [19]:
    df[(df['close']-df['open'])/df['open']>=0.03].index
    
    Out[19]:
    DatetimeIndex(['2001-08-27', '2001-08-28', '2001-09-10', '2001-12-21',
                   '2002-01-18', '2002-01-31', '2003-01-14', '2003-10-29',
                   '2004-01-05', '2004-01-14',
                   ...
                   '2018-06-20', '2018-08-09', '2018-08-21', '2018-08-27',
                   '2018-09-18', '2018-09-26', '2018-10-19', '2018-10-31',
                   '2018-11-13', '2018-12-28'],
                  dtype='datetime64[ns]', name='date', length=291, freq=None)
     

    输出该股票所有开盘比前日收盘跌幅超过2%的日期

    In [20]:
    df[(df['open']-df['close'].shift(1))/df['close'].shift(1)<=-0.02].index
    
    Out[20]:
    DatetimeIndex(['2001-09-12', '2002-06-26', '2002-12-13', '2004-07-01',
                   '2004-10-29', '2006-08-21', '2006-08-23', '2007-01-25',
                   '2007-02-01', '2007-02-06', '2007-03-19', '2007-05-21',
                   '2007-05-30', '2007-06-05', '2007-07-27', '2007-09-05',
                   '2007-09-10', '2008-03-13', '2008-03-17', '2008-03-25',
                   '2008-03-27', '2008-04-22', '2008-04-23', '2008-04-29',
                   '2008-05-13', '2008-06-10', '2008-06-13', '2008-06-24',
                   '2008-06-27', '2008-08-11', '2008-08-19', '2008-09-23',
                   '2008-10-10', '2008-10-15', '2008-10-16', '2008-10-20',
                   '2008-10-23', '2008-10-27', '2008-11-06', '2008-11-12',
                   '2008-11-20', '2008-11-21', '2008-12-02', '2009-02-27',
                   '2009-03-25', '2009-08-13', '2010-04-26', '2010-04-30',
                   '2011-08-05', '2012-03-27', '2012-08-10', '2012-11-22',
                   '2012-12-04', '2012-12-24', '2013-01-16', '2013-01-25',
                   '2013-09-02', '2014-04-25', '2015-01-19', '2015-05-25',
                   '2015-07-03', '2015-07-08', '2015-07-13', '2015-08-24',
                   '2015-09-02', '2015-09-15', '2017-11-17', '2018-02-06',
                   '2018-02-09', '2018-03-23', '2018-03-28', '2018-07-11',
                   '2018-10-11', '2018-10-24', '2018-10-25', '2018-10-29',
                   '2018-10-30'],
                  dtype='datetime64[ns]', name='date', freq=None)
     

    假如我从2010年1月1日开始,每月第一个交易日买入1手股票,每年最后一个交易日卖出所有股票,到今天为止,我的收益如何?

    In [21]:
    df['open'][-1]
    
    Out[21]:
    690.0
    In [22]:
    price_last = df['open'][-1]
    df = df['2010-01':'2019-01'] #剔除首尾无用的数据
    df
    
    Out[22]:
     openclosehighlow
    date    
    2010-01-04 109.760 108.446 109.760 108.044
    2010-01-05 109.116 108.127 109.441 107.846
    2010-01-06 107.840 106.417 108.165 106.129
    2010-01-07 106.417 104.477 106.691 103.302
    2010-01-08 104.655 103.379 104.655 102.167
    2010-01-11 104.400 102.926 105.230 102.422
    2010-01-12 103.028 105.708 106.040 102.492
    2010-01-13 104.649 103.022 105.389 102.741
    2010-01-14 103.379 107.552 107.974 103.379
    2010-01-15 107.533 108.401 110.641 107.533
    2010-01-18 108.484 109.110 109.926 108.420
    2010-01-19 109.116 108.337 109.441 108.165
    2010-01-20 108.427 105.881 108.580 105.804
    2010-01-21 105.842 106.397 107.450 105.613
    2010-01-22 106.314 104.738 106.755 103.309
    2010-01-25 104.560 105.957 106.761 103.704
    2010-01-26 107.380 106.378 108.593 105.038
    2010-01-27 105.951 104.643 107.068 104.400
    2010-01-28 104.566 107.974 108.708 104.336
    2010-01-29 108.452 107.552 108.612 107.004
    2010-02-01 107.769 107.776 108.216 106.576
    2010-02-02 107.208 106.263 108.484 106.117
    2010-02-03 106.066 105.887 107.272 104.783
    2010-02-04 105.868 107.591 108.006 105.376
    2010-02-05 106.959 109.282 109.684 106.570
    2010-02-08 109.282 109.269 112.058 108.816
    2010-02-09 109.760 109.193 110.609 108.822
    2010-02-10 109.760 109.652 110.137 108.931
    2010-02-11 109.633 110.641 111.318 109.505
    2010-02-12 111.018 110.456 111.164 109.888
    ... ... ... ... ...
    2018-12-07 595.600 587.000 598.860 584.700
    2018-12-10 576.000 577.230 587.500 574.180
    2018-12-11 577.000 582.990 586.550 577.000
    2018-12-12 588.790 589.000 589.200 582.800
    2018-12-13 590.000 601.750 606.880 589.600
    2018-12-14 598.010 592.960 599.800 592.000
    2018-12-17 592.570 590.520 595.970 585.010
    2018-12-18 585.880 590.940 591.540 585.000
    2018-12-19 589.000 577.800 590.990 576.030
    2018-12-20 576.220 574.500 581.500 568.890
    2018-12-21 571.000 567.410 573.450 562.210
    2018-12-24 563.500 568.000 570.910 558.000
    2018-12-25 562.220 565.790 567.500 553.610
    2018-12-26 563.810 560.080 568.400 558.000
    2018-12-27 570.000 563.000 571.850 562.220
    2018-12-28 563.300 590.010 596.400 560.000
    2019-01-02 609.980 598.980 612.000 595.010
    2019-01-03 599.970 590.000 601.660 585.800
    2019-01-04 587.280 602.000 607.670 582.020
    2019-01-07 608.000 605.490 612.000 602.220
    2019-01-08 605.500 604.790 612.000 600.280
    2019-01-09 609.990 616.120 626.160 609.040
    2019-01-10 617.130 618.770 624.880 610.250
    2019-01-11 625.000 635.880 637.000 618.750
    2019-01-14 633.000 624.600 635.890 621.550
    2019-01-15 627.860 659.980 661.500 625.000
    2019-01-16 658.000 661.100 665.650 653.600
    2019-01-17 663.000 659.250 669.700 655.800
    2019-01-18 665.000 683.610 690.200 661.500
    2019-01-21 690.000 686.980 698.880 681.880

    2195 rows × 4 columns

    In [28]:
    df_monthly = df.resample("M").first()
    df_monthly.head()
    
    Out[28]:
     openclosehighlow
    date    
    2010-01-31 109.760 108.446 109.760 108.044
    2010-02-28 107.769 107.776 108.216 106.576
    2010-03-31 106.219 106.085 106.857 105.925
    2010-04-30 101.324 102.141 102.422 101.311
    2010-05-31 81.676 82.091 82.678 80.974
    In [29]:
    df_yearly = df.resample("A").last()[:-1] #去除最后一年
    df_yearly
    
    Out[29]:
     openclosehighlow
    date    
    2010-12-31 117.103 118.469 118.701 116.620
    2011-12-31 138.039 138.468 139.600 136.105
    2012-12-31 155.208 152.087 156.292 150.144
    2013-12-31 93.188 96.480 97.179 92.061
    2014-12-31 157.642 161.056 161.379 157.132
    2015-12-31 207.487 207.458 208.704 207.106
    2016-12-31 317.239 324.563 325.670 317.239
    2017-12-31 707.948 687.725 716.329 681.918
    2018-12-31 563.300 590.010 596.400 560.000
    In [31]:
    df_monthly.loc[str(year)]
    
    Out[31]:
     openclosehighlow
    date    
    2018-01-31 690.200 693.996 700.218 680.232
    2018-02-28 756.262 747.122 756.558 742.379
    2018-03-31 717.808 731.582 736.394 713.637
    2018-04-30 670.480 670.539 681.326 664.673
    2018-05-31 650.760 658.480 659.624 636.029
    2018-06-30 740.614 734.679 744.410 728.417
    2018-07-31 734.520 711.550 739.330 703.000
    2018-08-31 731.400 714.940 732.300 714.110
    2018-09-30 652.000 666.210 667.670 650.800
    2018-10-31 715.410 686.150 719.000 686.150
    2018-11-30 555.000 563.000 585.500 551.250
    2018-12-31 589.000 601.200 605.000 584.770
    In [33]:
    cost_money = 0
    hold = 0 #每年持有的股票
    for year in range(2010, 2020):
        
        cost_money -= df_monthly.loc[str(year)]['open'].sum()*100
        hold += len(df_monthly[str(year)]['open']) * 100
        if year != 2019:
            cost_money += df_yearly[str(year)]['open'][0] * hold
            hold = 0 #每年持有的股票
        #print(cost_money)
    
    cost_money += hold * price_last
    
    print(cost_money)
            
    # df_monthly['2001']
    
     
    289604.69999999984
    
    In [56]:
    df['2017']
    
    Out[56]:
     openclosehighlow
    date    
    2017-01-03 329.299 329.575 331.979 327.851
    2017-01-04 329.634 346.667 346.923 329.615
    2017-01-05 344.785 341.574 346.213 340.293
    2017-01-06 341.475 345.534 354.419 340.943
    2017-01-09 342.618 343.317 347.622 341.377
    2017-01-10 343.258 343.800 346.755 341.436
    2017-01-11 342.815 340.303 342.815 338.382
    2017-01-12 341.386 341.879 342.224 339.377
    2017-01-13 341.810 339.731 342.214 338.756
    2017-01-16 339.003 336.382 339.663 333.752
    2017-01-17 337.495 343.928 346.263 336.904
    2017-01-18 343.682 349.789 351.454 342.037
    2017-01-19 349.711 349.435 353.139 346.460
    2017-01-20 349.612 349.701 352.075 347.770
    2017-01-23 351.563 345.987 354.439 344.253
    2017-01-24 346.036 345.090 347.543 341.583
    2017-01-25 344.765 342.450 344.765 341.042
    2017-01-26 342.953 339.288 343.603 336.215
    2017-02-03 340.845 341.682 343.977 339.860
    2017-02-06 343.298 341.682 343.692 339.820
    2017-02-07 341.672 338.461 342.224 338.027
    2017-02-08 338.638 339.081 340.352 337.564
    2017-02-09 339.860 342.431 343.593 339.475
    2017-02-10 343.426 339.938 344.608 339.860
    2017-02-13 338.874 344.539 346.657 336.825
    2017-02-14 344.332 344.992 347.346 343.898
    2017-02-15 346.263 343.337 347.189 342.027
    2017-02-16 343.307 342.904 343.426 340.362
    2017-02-17 343.701 345.337 349.159 342.322
    2017-02-20 345.189 355.907 356.921 344.795
    ... ... ... ... ...
    2017-10-20 581.000 574.330 581.000 571.760
    2017-10-23 574.920 573.410 580.500 568.000
    2017-10-24 572.000 567.330 573.800 563.610
    2017-10-25 569.750 565.670 571.680 562.060
    2017-10-26 605.000 605.090 613.000 591.910
    2017-10-27 602.110 649.630 655.000 600.030
    2017-10-30 643.050 622.080 644.000 621.000
    2017-10-31 622.010 618.030 627.600 611.880
    2017-11-01 620.880 623.010 631.540 619.220
    2017-11-02 626.950 626.920 629.780 621.000
    2017-11-03 628.650 639.170 643.440 628.650
    2017-11-06 641.500 653.060 657.620 640.200
    2017-11-07 650.580 642.070 657.150 638.180
    2017-11-08 642.330 650.380 654.500 642.330
    2017-11-09 648.000 650.070 654.150 644.610
    2017-11-10 650.430 677.950 683.400 650.430
    2017-11-13 680.000 687.880 692.780 675.800
    2017-11-14 687.140 678.750 687.220 677.350
    2017-11-15 678.750 688.080 689.990 674.000
    2017-11-16 686.010 719.110 719.960 686.010
    2017-11-17 696.000 690.250 709.000 677.770
    2017-11-20 676.800 679.150 685.600 660.000
    2017-11-21 669.000 677.250 688.800 662.020
    2017-11-22 677.000 650.520 688.000 650.000
    2017-11-23 642.000 633.730 646.000 630.220
    2017-11-24 635.900 630.040 642.500 622.230
    2017-11-27 634.000 621.290 636.330 613.010
    2017-11-28 625.060 648.230 648.880 624.000
    2017-11-29 649.940 638.120 651.970 634.010
    2017-11-30 635.510 631.000 648.480 626.000

    223 rows × 4 columns

  • 相关阅读:
    POJ 2723 Get Luffy Out(2-SAT)
    ZOJ 3613 Wormhole Transport
    HDU 4085 Peach Blossom Spring
    NBUT 1221 Intermediary
    NBUT 1223 Friends number
    NBUT 1220 SPY
    NBUT 1218 You are my brother
    PAT 1131. Subway Map (30)
    ZSTU OJ 4273 玩具
    ZSTU OJ 4272 最佳淘汰算法
  • 原文地址:https://www.cnblogs.com/bilx/p/11611899.html
Copyright © 2011-2022 走看看