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__)
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]:
In [33]:
df = pd.DataFrame(data = BabyDataSet, columns=['Names', 'Births'])
df
Out[33]:
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]:
In [39]:
df = pd.read_csv(Location, header=None)
df
Out[39]:
In [40]:
df = pd.read_csv(Location, names=['Names','Births'])
df
Out[40]:
In [23]:
# Check data type of the columns
df.dtypes
Out[23]:
In [22]:
# Check data type of Births column
df.Births.dtype
Out[22]:
In [24]:
# Method 1:
Sorted = df.sort_values(['Births'], ascending=False)
Sorted.head(1)
Out[24]:
In [25]:
# Method 2:
df['Births'].max()
Out[25]:
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
Out[26]:
In [ ]: