In [27]:
# SEE EXAMPLES: https://bitbucket.org/hrojas/learn-pandas

In [28]:
# Import all libraries needed for the tutorial

# General syntax to import specific functions in a library: 
##from (library) import (specific library function)
from pandas import DataFrame, read_csv

# General syntax to import a library but no functions: 
##import (library) as (give the library a nickname/alias)
import matplotlib.pyplot as plt
import pandas as pd #this is how I usually import pandas
import sys #only needed to determine Python version number
import matplotlib #only needed to determine Matplotlib version number

# Enable inline plotting
%matplotlib inline

In [29]:
print('Python version ' + sys.version)
print('Pandas version ' + pd.__version__)
print('Matplotlib version ' + matplotlib.__version__)


Python version 3.5.2 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:53:06) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
Pandas version 0.19.2
Matplotlib version 1.5.3

In [30]:
# The inital set of baby names and bith rates
names = ['Bob','Jessica','Mary','John','Mel']
births = [968, 155, 77, 578, 973]

In [31]:
zip?

In [32]:
BabyDataSet = list(zip(names,births))
BabyDataSet


Out[32]:
[('Bob', 968), ('Jessica', 155), ('Mary', 77), ('John', 578), ('Mel', 973)]

In [33]:
df = pd.DataFrame(data = BabyDataSet, columns=['Names', 'Births'])
df


Out[33]:
Names Births
0 Bob 968
1 Jessica 155
2 Mary 77
3 John 578
4 Mel 973

In [34]:
df.to_csv?

In [35]:
df.to_csv('births1880.csv',index=False,header=False)

In [36]:
read_csv?

In [37]:
Location = 'births1880.csv'
df = pd.read_csv(Location)

In [38]:
df


Out[38]:
Bob 968
0 Jessica 155
1 Mary 77
2 John 578
3 Mel 973

In [39]:
df = pd.read_csv(Location, header=None)
df


Out[39]:
0 1
0 Bob 968
1 Jessica 155
2 Mary 77
3 John 578
4 Mel 973

In [40]:
df = pd.read_csv(Location, names=['Names','Births'])
df


Out[40]:
Names Births
0 Bob 968
1 Jessica 155
2 Mary 77
3 John 578
4 Mel 973

In [23]:
# Check data type of the columns
df.dtypes


Out[23]:
Names     object
Births     int64
dtype: object

In [22]:
# Check data type of Births column
df.Births.dtype


Out[22]:
dtype('int64')

In [24]:
# Method 1:
Sorted = df.sort_values(['Births'], ascending=False)
Sorted.head(1)


Out[24]:
Names Births
4 Mel 973

In [25]:
# Method 2:
df['Births'].max()


Out[25]:
973

In [26]:
# Create graph
df['Births'].plot()

# Maximum value in the data set
MaxValue = df['Births'].max()

# Name associated with the maximum value
MaxName = df['Names'][df['Births'] == df['Births'].max()].values

# Text to display on graph
Text = str(MaxValue) + " - " + MaxName

# Add text to graph
plt.annotate(Text, xy=(1, MaxValue), xytext=(8, 0), 
                 xycoords=('axes fraction', 'data'), textcoords='offset points')

print("The most popular name")
df[df['Births'] == df['Births'].max()]
#Sorted.head(1) can also be used


The most popular name
Out[26]:
Names Births
4 Mel 973

In [ ]: