import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

"""
Explanation: A  ten minute tour of Pandas
End of explanation
"""


# make a list of random numbers
a_python_list = list(np.random.randn(5))
a_python_list

a_pandas_series = pd.Series(data=a_python_list)
a_pandas_series

"""
Explanation: Two Important Data Structures

Series - a one-dimensional labeled array or list. Like an independant column in a spreadsheet 
Dataframe - a two-dimensional labeled table. Like the whole spreadsheet
the Index - Ok, there are THREE important data structures. The index are the labels 

Built on top of Numpy

Which is a lower-level library for manipulating numerical data
Implemented in C, which means it is wicked fast
Pandas is more flexible and useful than Numpy

Series

s = pd.Series(data, index=index)
data can be
A Python Dictionary
A list
A single value


index is a list that will become item lables (like the keys in a dictionary
Think of a Series as an ordered dictionary, which means it is very list-like
End of explanation
"""


a_simple_index = ['a', 'b', 'c', 'd', 'e']
a_pandas_series = pd.Series(data=a_python_list, index=a_simple_index)
a_pandas_series

"""
Explanation: You can see there is the list of random numbers
There is also a list of integers, the index
And there is a data type dype associated with the items in the list
End of explanation
"""


# index by label
a_pandas_series['a']

# index by location
a_pandas_series[1]

"""
Explanation: Notice how the index has changed from numbers to letters
we can use the index to extract specific values from the Seres
End of explanation
"""


a_python_dictionary = {'a' : 0., 'b' : 1., 'c' : 2.}
pd.Series(a_python_dictionary)

"""
Explanation: you can also create a Series from a python dictionary
End of explanation
"""


a_big_series = pd.Series(np.random.randn(1000))
a_big_series

a_big_series * 2

a_big_series.sum() / len(a_big_series)

a_big_series.mean()

a_big_series.describe()

"""
Explanation: Series are useful for doing fast, "vectorized" operations 
Much faster than doing this with Python data structures
End of explanation
"""


a_dictionary = {'one' : [1., 2., 3., 4.],
                'two' : [4., 3., 2., 1.]}

a_dataframe = pd.DataFrame(a_dictionary)
a_dataframe    

a_dataframe = pd.DataFrame(a_dictionary,
                           index=['a', 'b', 'c', 'd'])
a_dataframe

"""
Explanation: Dictionaries

DataFrame is a 2-dimensional labeled data structure with columns of potentially different types. 
You can think of it like a spreadsheet or R dataframe
Most popular data structure
End of explanation
"""


a_list_of_dictionaries = [{'a': 1, 'b': 2}, {'a': 5, 'b': 10, 'c': 20}]
a_dataframe = pd.DataFrame(a_list_of_dictionaries)
a_dataframe

"""
Explanation: Very commonly you can create a Dataframe from a list of dictionaries
End of explanation
"""


a_dataframe['a']

"""
Explanation: We can select columns by using python slicing
End of explanation
"""


a_dataframe = pd.DataFrame({'a': np.random.randn(1000),
                            'b': np.random.randn(1000),
                            'c': np.random.randn(1000),
                            'd': np.random.randn(1000),
                            'e': 'hi'})
a_dataframe

a_dataframe.dtypes

a_dataframe.describe()

"""
Explanation: This gives us column 'a' as a Series data structure
End of explanation
"""


a_dataframe.to_csv("random-data.csv") 

"""
Explanation: Dataframes are very useful for reading and writing tabular data to disk
We can save this dataframe as a CSV or an Excel spreadsheet
End of explanation
"""


a_dataframe.to_excel("random-data.xls")

"""
Explanation: Now go back to the directory tree
You should see a new file called "random-data.csv"
End of explanation
"""


a_dataframe.to_excel("random-data.xls", index=False)
a_dataframe.to_csv("random-data.csv", index=False) 

"""
Explanation: Now you can open your data in excel, how fun!
End of explanation
"""


a_new_dataframe = pd.read_csv("random-data.csv")
a_new_dataframe

a_new_dataframe.dtypes

"""
Explanation: Reading data is also super easy
End of explanation
"""


a_new_dataframe.plot()

a_new_dataframe['a'].plot()

"""
Explanation: Plotting with Pandas

One of the other nice features of Pandas is how easy it is to create charts and graphs from Series and Dataframes
This is because of tight integration with Matplotlib, which is the most popular plotting library for python
End of explanation
"""


a_new_dataframe.plot(kind="box")

a_new_dataframe.plot(kind="density")

a_new_dataframe.hist()

"""
Explanation: There are a lot of options for the plot() function
The pandas documentation for dataframe.plot() has a lot of good information
End of explanation
"""


