[Python] Read and plot data from csv file

Install:

pip install pandas
pip install matplotlib # check out the doc from site
import pandas as pd
import matplotlib.pyplot as plt
from numpy import mean

def load_df(symbol):
    return pd.read_csv("data/{0}.csv".format(symbol))

def get_max_close(symbol):
    """ Return the maximum closing value for stock idicicated by symbol.
    Note: Data for a stock is stored in file: data/<symbol>.csv
    """
    df = load_df(symbol)
    return df["Close"].max()

def get_mean_volume(symbol):
    df = load_df(symbol)
    return mean(df[‘Volume‘])

def printAdjClose():
    df = pd.read_csv(‘data/ibm.csv‘)
    df[[‘Low‘, ‘High‘]].plot()
    plt.show()

def test_run():
    """
    Function called by Test Run
    """
    for symbol in [‘aapl‘, ‘ibm‘]:
        print("Max close")
        print(symbol, get_max_close(symbol))
        print("Mean Volume")
        print(symbol, get_mean_volume(symbol))

if __name__ == "__main__":
    test_run()
    printAdjClose()
时间: 2024-11-10 14:27:20

[Python] Read and plot data from csv file的相关文章

How to handle csv file using python

As i need to read something from a csv file using python.  I got something and put it here. Module: csv import csv FILE_FULL_PATH = 'D:\\Work\\script\\My Script\\Book1.csv' def f(): with open(FILE_FULL_PATH,'rb') as csvfile: for row in csv.reader(csv

NetSuite SuiteScript 2.0 export data to Excel file(xls)

In NetSuite SuiteScript, We usually do/implement export data to CSV, that's straight forward: Collect 'encoded' string to Array for column, join them with comma ',' to be a string. Collect each line's data same as column to push to the Array. Join al

Python Tutorial 学习(五)--Data Structures

5. Data Structures 这一章来说说Python的数据结构 5.1. More on Lists 之前的文字里面简单的介绍了一些基本的东西,其中就涉及到了list的一点点的使用.当然,它可不仅仅只有那么一点点,这里给出一个更详细一点的说明.来吧骚连,打开你的命令行窗口 >>>help(list) 看看会出来一些什么~~` list.append(x) 向一个序列里面追加元素 x a = [] a.append(x) # 假设x已经定义了 a[len(a):] = [x] l

a helper class for generating xls or csv file

using System;using System.Collections.Generic;using System.Linq;using System.Text;using Microsoft.Office.Interop.Excel; namespace Reuters.ProcessQuality.ContentAuto.Lib{ public class XlsOrCsvUtil { /// <summary> /// Generate Xls Or Csv File /// <

python之小应用:读取csv文件并处理01数据串

目的:读取csv文件内容,把0和1的数据串取出来,统计出现1的连续次数和各次数出现的频率次数 先读取csv文件内容: import csv def csv_read(file): list = [] csv_reader = csv.reader(file) for id, data, *args in csv_reader: #跳过表头 if id == " ": continue #print(id, data) list.append(data) return list 再写处理

python 遇到 syntaxerror: non-ascii character &#39;/xd6&#39; in file 我 教你解决 (python问题)(转)

遇到标题上的问题,按照我的解决广方案来吧 ===================================== (一) 用记事本创建一个文件ChineseTest.py,默认ANSI: s = "中文" print s测试一下瞧瞧: E:/Project/Python/Test>python ChineseTest.py File "ChineseTest.py", line 1 SyntaxError: Non-ASCII character '/xd

利用php CI force_download($filename, $data) 下载.csv 文件解决文件名乱码,文件内容乱码

利用php CI force_download($filename, $data) 下载.csv 文件解决文件名乱码,文件内容乱码.做了很久终于知道了很好的解决方案. 1.加载辅助函数 $this->load->helper('download'); //下载辅助函数 $this->load->helper('string'); //字符编码转换辅助翻书 2.force_download($filename, $data)通过它的代码可以知道$data 必须是字符串,如果不是字符串

PHP class to export data in CSV, TSV, or Excel XML

<?php // Example of exporting a large amount of data to a file. On my  // computer it takes 43 seconds to write out 83MB of data, but  // only uses 750kb of memory. set_time_limit(0);  ini_set('memory_limit','-1'); require "../php-export-data.clas

【Python】SyntaxError: Non-ASCII character &#39;\xe8&#39; in file

遇到的第一个问题: SyntaxError: Non-ASCII character '\xe8' in file D:/PyCharmProject/TempConvert.py on line 2, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details 原因:注释里面出现了中文,而 Python 支持的 ASCII 码无中文. 解决方法:在头文件中添加如下代码: # -*- coding: