Class03 Answer:

Write Pandas syntax which mimics this SQL:

SELECT COUNT(cdate) FROM prices;
SELECT COUNT(lowp)  FROM prices;
SELECT COUNT(*)     FROM prices;
SELECT MIN(cdate),MIN(closep),MIN(volume) FROM prices;
SELECT MAX(closep),MAX(volume), AVG(closep),AVG(volume) FROM prices;

I did this lab by writing the script listed below:


"""
class03pd10.py

This script should mimic the SQL listed below:

SELECT COUNT(cdate) FROM prices;
SELECT COUNT(lowp)  FROM prices;
SELECT COUNT(*)     FROM prices;
SELECT MIN(cdate),MIN(closep),MIN(volume) FROM prices;
SELECT MAX(closep),MAX(volume), AVG(closep),AVG(volume) FROM prices;

SQL lacks the ability to get data from the web.
Pandas, however, can get data from the web.
"""

import pandas as pd

prices_df = pd.read_csv('http://ml4.us/csv/ibm.csv')

# Mimic
# SELECT COUNT(cdate) FROM prices;
print(prices_df.cdate.count())

# Mimic
# SELECT COUNT(lowp) FROM prices;
print(prices_df.lowp.count())

# Mimic
# SELECT COUNT(*) FROM prices;
print(len(prices_df))

# Mimic
# SELECT MIN(cdate),MIN(closep),MIN(volume) FROM prices;
print(prices_df.cdate.min(), prices_df.closep.min(), prices_df.volume.min())

# Mimic
# SELECT MAX(closep),MAX(volume), AVG(closep),AVG(volume) FROM prices;
print(prices_df.closep.max(),prices_df.volume.max(),prices_df.closep.mean(),prices_df.volume.mean())

'bye'

I saw this:


dan@h79:~/ml4/public/class03demos $ python class03pd10.py 
13960
13960
13960
1962-01-02 41.0 0
649.0 69444700 189.64730139161958 4870000.866762178
dan@h79:~/ml4/public/class03demos $ 
dan@h79:~/ml4/public/class03demos $ 

Class03 Lab


ml4.us About Blog Contact Class01 Class02 Class03 Class04 Class05 Class06 Class07 Class08 Class09 Class10 dan101 Forum Google Hangout Vboxen