• Pandas 的基本操作


    原始数据结构如下:

    code date close prince
    0 a 1988-01-04 525.11 23
    1 a 1988-01-05 532.04 24
    2 a 1988-01-06 527.89 25
    3 a 1988-01-07 538.28 26
    4 a 1988-01-08 540.28 27
    5 a 1988-01-11 548.18 28
    6 a 1988-01-12 552.76 29
    7 b 1988-02-16 647.56 51
    8 b 1988-02-17 648.95 52
    9 b 1988-02-19 658.75 53
    10 b 1988-02-22 663.03 54
    11 b 1988-02-23 658.32 55
    12 c 1988-05-06 645.69 22
    13 c 1988-05-09 639.77 23
    14 c 1988-05-10 635.47 24
    15 c 1988-05-11 634.05 25
    16 c 1988-05-12 636.68 26
    17 c 1988-05-13 650.66 27
    18 c 1988-05-16 657.57 28
    19 c 1988-05-17 658.65 29
    20 c 1988-05-18 670.31 30
    21 c 1988-05-19 678.77 31
    22 c 1988-05-20 678.24 32
    23 c 1988-05-24 698.29 33
    24 c 1988-05-25 705.51 34
    25 c 1988-05-26 703.54 35
    26 c 1988-05-27 716.85 36
    27 d 1988-07-06 703.15 61
    28 d 1988-07-07 707.02 62
    29 d 1988-07-08 701.41 63
    30 d 1988-07-11 702.52 64
    31 d 1988-07-12 696.11 65
    32 d 1988-07-13 692.79 66
    33 d 1988-07-14 687.36 67
    34 d 1988-07-15 690.86 68
    35 d 1988-07-18 678.95 69

    1, 累加某一列值(部分输出)

    def __f1(x):
    x['bp'] = x['close'].cumsum()
    return x
    df = pd.read_excel("chg.xlsx")
    df= df.groupby(['code']).apply(__f1)
    print df

    code date close prince bp
    0 a 1988-01-04 525.11 23 525.11
    1 a 1988-01-05 532.04 24 1057.15
    2 a 1988-01-06 527.89 25 1585.04
    3 a 1988-01-07 538.28 26 2123.32
    4 a 1988-01-08 540.28 27 2663.60
    5 a 1988-01-11 548.18 28 3211.78




    2, shift 函数:在x或y轴上进行平移(部分输出)
    def __f1(x):
    x['bp'] = x['close'] - x['close'].shift(1)
    return x
    df = pd.read_excel("chg.xlsx")
    df= df.groupby(['code']).apply(__f1)
    print df


    code date close prince bp
    0 a 1988-01-04 525.11 23 NaN
    1 a 1988-01-05 532.04 24 6.93
    2 a 1988-01-06 527.89 25 -4.15
    3 a 1988-01-07 538.28 26 10.39
    4 a 1988-01-08 540.28 27 2.00
    5 a 1988-01-11 548.18 28 7.90
    6 a 1988-01-12 552.76 29 4.58
    7 b 1988-02-16 647.56 51 NaN

    3,排序

    def __f1(x):
    x['bp'] = x['close'].sort_values(ascending=False)
    return x
    df = pd.read_excel("chg.xlsx")
    df= df.groupby(['code']).apply(__f1)
    print df

    code date close prince bp
    0 a 1988-01-04 525.11 23 525.11
    1 a 1988-01-05 532.04 24 532.04
    2 a 1988-01-06 527.89 25 527.89
    3 a 1988-01-07 538.28 26 538.28





  • 相关阅读:
    USACO Broken Necklace 通过了
    USACO Broken Necklace
    推荐顺序ACM
    usaco暂时无法访问
    格式
    稳定排序
    归并排序
    浅析Struts1和Struts2的Action线程安全问题
    判别式模型与生成式模型的区别
    远景能源一面
  • 原文地址:https://www.cnblogs.com/Rubick7/p/8593528.html
Copyright © 2020-2023  润新知