zoukankan      html  css  js  c++  java
  • Python可视化

    题目:
    读取 stock_hist_data.csv 中招商银行(股票代码 600036)2018 年下半年的
    股票数据并完成如下数据处理和分析任务:
    1、 数据只保留 date、open、high、close、low 和 volume 这几个属性,并按
    时间先后顺序对数据进行排序;使用 matplotlib 绘制出收盘价(close)的走势折
    线图。
    2、 输出这半年内成交量(volume)最低和最高那两天的日期和分别的成交
    量;
    3、 列出成交量(volume)在 1000000 以上的记录;
    4、 计算这半年中收盘价(close)高于开盘价(open)的天数;
    5、 计算每月收盘价的平均值,并使用 matplotlib 绘制出柱状图。

    import pandas as pd
    from pandas import to_datetime
    import matplotlib.pyplot as plt


    #读数据
    data_read = pd.DataFrame(pd.read_csv(open("data.csv",'r')))
    data_read.drop(['price_change','p_change','ma5','ma10','ma20','v_ma5','v_ma10','v_ma20'],axis=1,inplace=True)
    print(data_read)
    #转化为python可以识别的日期格式
    data_read['date'] = pd.to_datetime(data_read['date'])
    #按照时间戳进行排序
    data_read.sort_values(by = ['date'],ascending = True ,inplace = True)

    #绘制折线图
    plt.plot(data_read['date'],data_read['close'])
    plt.xticks()
    plt.xlabel("date")#x轴的文字说明
    plt.ylabel("close")#y轴文字说明
    plt.title("date-close")#此图像的标题
    plt.show()
    plt.close()

    #输出volume最大的和最小的
    data_read.sort_values(by = 'volume',ascending=True,inplace=True)
    volume_max = data_read[-1:]
    volume_min = data_read[0:1]
    print(volume_max)
    print(volume_min)

    #成交量在1000000以上的数据
    volume_data = data_read[data_read['volume']>1000000]
    print(volume_data)

    #收盘价高于开盘价的天数
    open_data = data_read['open']
    close_data = data_read['close']
    s=0
    for i,op in enumerate(close_data):
    if op > open_data[i]:
    s = s + 1;
    print("收盘价高于开盘价的天数为:",s)


    data_read['date']=pd.to_datetime(data_read['date'])
    data_read = data_read.set_index('date')
    volume_avg=[]

    seven_volume = data_read['2018-07']
    seven_temp = seven_volume['close']
    seven_avg = seven_temp.mean(axis=0)
    volume_avg.append(seven_avg)

    eight_volume = data_read['2018-08']
    eight_temp = eight_volume['close']
    eight_avg = eight_temp.mean(axis=0)
    volume_avg.append(eight_avg)


    nine_volume = data_read['2018-09']
    nine_temp = nine_volume['close']
    nine_avg = nine_temp.mean(axis=0)
    volume_avg.append(nine_avg)

    ten_volume = data_read['2018-10']
    ten_temp = ten_volume['close']
    ten_avg = ten_temp.mean(axis=0)
    volume_avg.append(ten_avg)

    eleven_volume = data_read['2018-11']
    eleven_temp = eleven_volume['close']
    eleven_avg = eleven_temp.mean(axis=0)
    volume_avg.append(eleven_avg)

    twelve_volume = data_read['2018-12']
    twelve_temp = twelve_volume['close']
    twelve_avg = twelve_temp.mean(axis=0)
    volume_avg.append(twelve_avg)

    print("七到十二月的平均收盘价为:",volume_avg)

    name_list = ['July','Auguest','September','October','November','December']
    plt.bar(range(len(volume_avg)),volume_avg,color = 'rgb',tick_label = name_list)
    plt.show()



    如有不对之处,请多多读指教
  • 相关阅读:
    Python学习笔记(尚硅谷)——变量
    四级高频词-工作类
    转载--JS根据浏览器的useAgent来判断浏览器的类型
    MySQL上传文件容量过大com.mysql.jdbc.PacketTooBigException
    关于<input type="file" >浏览器兼容问题
    IDictionary与TryGetValue
    生成自增ID列
    利用C#将PCM格式的Wav进行对文件裁剪截取、淡入淡出、保存为音频文件相关详细代码解释
    DevExpress控件学习总结
    C# 音频操作系统项目总结
  • 原文地址:https://www.cnblogs.com/sunflowers-lanqijiu/p/11925232.html
Copyright © 2011-2022 走看看