30.    Python language and its Package Pandas for Data Analysis

December 09, 2018
home

Contents

00. intro to pandas

01. my data

        #dictionary to df
        import pandas as pd

        print("12/04/2018, 11:33 am")

        access_data = {'Day':[1,2,3,4,5],
                     'Visitors':[99,22,33,44,11],
                     'Bounce Rate':[15,20,30,40,50]}

        df = pd.DataFrame(access_data)


        # modify df to make Day as the index
        df.set_index('Day', inplace=True)
        print(df)

        import matplotlib.pyplot as plt
        from matplotlib import style
        style.use('fivethirtyeight')

        df.plot()
        #plt.show()
        plt.savefig('pandas_p01.png')
            

02. stock data from yahoo

        #pandas_02_yahoo.py
        print("test at 5:30 pm")

        import pandas as pd
        import datetime
        import pandas_datareader.data as web

        start = datetime.datetime(2015, 1, 1)
        end = datetime.datetime.now()
        
        df = web.DataReader("AAPL", "yahoo", start, end)
        
        print(df.head())

        import matplotlib.pyplot as plt
        from matplotlib import style
        style.use('fivethirtyeight')

        df['High'].plot()
        plt.legend()
        #plt.show()
        plt.savefig('pandas_02_yahoo.png')
        

03. create a dataframe from a csv file

        import pandas as pd
        df = pd.read_csv('mydata.csv') 
        print(df)           
            

04. installing python3.7 + pandas + other package, using code visual studio

        import numpy
        import pandas
        import matplotlib
        print("So far so good")
            

demo - using code visual studo, after Anconda3 installed

        # using w3c python tutorial example for if...elif...else

        a = 200
        b = 33
        if b > a:
          print("b is greater than a")
        elif a == b:
          print("a and b are equal")
        else:
          print("a is greater than b")