Plotting data from CSV in python
Question: I have CSV files in following format in a folder.It also have additional
column which I dont care

    
    
    Date        Price
    20150101     1
    20160102     3    
    

I want to iterate through all the files in folder and create graph for date on
x-axis and price on y-axis and save image on each page of pdf file.

I am fairly new to python and tried few things from google but it didn't work.

Thanks in advance


Answer: You can iterate over the list of files and save each plot as you go using
usecols to specify the column to use:

    
    
    import  pandas as pd
    import matplotlib.pyplot as plt
    import  os
    
    pth = "path_to"
    for fle in os.listdir(pth):
        df = pd.read_csv(os.path.join(pth, fle),usecols=(0, 1))
        df.plot()
        plt.savefig("{}.png".format(fle))
    

You can use [PdfPages](http://matplotlib.org/faq/howto_faq.html#save-multiple-
plots-to-one-pdf-file):

    
    
    from matplotlib.backends.backend_pdf import PdfPages
    import matplotlib.pyplot as plt
    import  pandas as pd
    import  glob
    with PdfPages('multipage_pdf.pdf') as pdf:
        pth = "path_to"
        for fle in os.listdir(pth):
            df = pd.read_csv(os.path.join(pth, fle),usecols=(0, 1)
            df.plot()
            pdf.savefig()
            plt.close()
    

