In [1]:
# Import all libraries needed for the tutorial
import pandas as pd
from numpy import random
import matplotlib.pyplot as plt
import sys #only needed to determine Python version number
# Enable inline plotting
%matplotlib inline
In [2]:
# The inital set of baby names
names = ['Bob','Jessica','Mary','John','Mel']
In [3]:
# This will ensure the random samples below can be reproduced.
# This means the random samples will always be identical.
random.seed?
In [4]:
random.seed(500)
random_names = [names[random.randint(low=0,high=len(names))] for i in range(1000)]
# Print first 10 records
random_names[:10]
Out[4]:
In [5]:
# The number of births per name for the year 1880
births = [random.randint(low=0,high=1000) for i in range(1000)]
births[:10]
Out[5]:
In [6]:
BabyDataSet = list(zip(random_names,births))
BabyDataSet[:10]
Out[6]:
In [7]:
df = pd.DataFrame(data = BabyDataSet, columns=['Names', 'Births'])
df[:10]
Out[7]:
In [8]:
df.to_csv('births1880.txt',index=False,header=False)
In [10]:
location = 'births1880.txt'
df = pd.read_csv(location)
In [11]:
df.info()
In [12]:
df.head()
Out[12]:
In [14]:
df = pd.read_csv(location, header=None)
df.info()
In [15]:
df.tail()
Out[15]:
In [16]:
df = pd.read_csv(location, names=['Names','Births'])
df.head(5)
Out[16]:
In [17]:
import os
os.remove(location)
In [18]:
# Method 1:
df['Names'].unique()
Out[18]:
In [20]:
# If you actually want to print the unique values:
for x in df['Names'].unique():
print(x)
In [22]:
# Method 2:
print(df['Names'].describe())
In [23]:
# Create a groupby object
name = df.groupby('Names')
# Apply the sum function to the groupby object
df = name.sum()
df
Out[23]:
In [24]:
# Method 1:
Sorted = df.sort(['Births'], ascending=False)
Sorted.head(1)
Out[24]:
In [25]:
# Method 2:
df['Births'].max()
Out[25]:
In [27]:
# Create graph
df['Births'].plot(kind='bar')
print("The most popular name")
df.sort(columns='Births', ascending=False)
Out[27]:
In [ ]: