01. my data
steps
- Use my created data for this demo.
- create a file in any editor, pandas_01_mydata.py as below.
- in browser, open rept.it/languages/python3
- click ther icon for my repl, select a new.
- create a subsirectory, mydir
- upload the create py file into mydir.
- click package icon, add package pandas-datareader.
- update main.py
print("enter main.py")
from mydir import pandas_01_mydata
- click run
- If the image does not show, edit a bit, like print("12/04/2018 11:45") on the new py file to trigger, kind of patch.
#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')
code review
- The site is repl.it/languages/python3.
- package python3, and its related packages - numpy, matplotlib are not required to prepare.
- line 6, create a dictionary object
- key is a string.
- value is a list.
- line 10, create a dataframe from the dictionary object.
- line 14, set 'Day' as the index.
- line 15, print(df), the data is presented in a tabular form.It demonstrates to use a dataframe.
- line 19, style.use('fivethirtyeight') gives a better look.
- line 21, plot
- line 22, comment out because this is on the web.
- line 23, save it as an image file instead.It demonstrate for data virtualization.
02. stock data from yahoo
steps
- Use the data from yahoo for this demo.
- create a file in any editor, pandas_02_yahoo.py as below.
- in browser, open rept.it/languages/python3
- click ther icon for my repl, select the one for the previous lab.
- upload the create py file into mydir.
- update main.py
print("enter main.py")
from mydir import pandas_02_yahoo
- click run
- If the image does not show, edit a bit, like print("test at5:32") on the new py file to trigger, kind of patch.
- The py code is as below:
#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')
- The code highlighted in red is the main code.
- pandas_datareader.data is a module.
- in its function DataReader
- argument 1: "AAPL" is the stock symbol.
- argument 2: data from Yahoo.
- argument 3,4: the date range
- return in a dataframe.
- You can see the output and the plotting.
- This way is similar to accessing web services.
- All are in one FUNCTION CALL.
- It takes care of its connection and prototocals.