Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:24:07

0001 #!/usr/bin/env python3
0002 
0003 from __future__ import print_function
0004 import matplotlib
0005 matplotlib.use('Agg')
0006 import matplotlib.pyplot as pyplot
0007 
0008 
0009 ## General syntax to import specific functions in a library: 
0010 ##from (library) import (specific library function)
0011 from pandas import DataFrame, read_csv
0012 
0013 # General syntax to import a library but no functions: 
0014 ##import (library) as (give the library a nickname/alias)
0015 import matplotlib.pyplot as plt
0016 import pandas as pd #this is how I usually import pandas
0017 import sys #only needed to determine Python version number
0018 import matplotlib #only needed to determine Matplotlib version number
0019 
0020 print('Python version ' + sys.version)
0021 print('Pandas version ' + pd.__version__)
0022 print('Matplotlib version ' + matplotlib.__version__)
0023 
0024 # The inital set of baby names and bith rates
0025 names = ['Bob','Jessica','Mary','John','Mel']
0026 births = [968, 155, 77, 578, 973]
0027 
0028 BabyDataSet = list(zip(names,births))
0029 print(BabyDataSet)
0030 
0031 df = pd.DataFrame(data = BabyDataSet, columns=['Names', 'Births'])
0032 print(df)
0033 
0034 df.to_csv('births1880.csv',index=False,header=False)
0035 
0036 Location = './births1880.csv'
0037 df = pd.read_csv(Location)
0038 
0039 print(df)
0040 
0041 df = pd.read_csv(Location, names=['Names','Births'])
0042 print(df)
0043 
0044 print(df.dtypes)
0045 
0046 # Check data type of Births column
0047 print(df.Births.dtype)
0048 
0049 # Method 1:
0050 Sorted = df.sort_values(['Births'], ascending=False)
0051 print(Sorted.head(1))
0052 
0053 # Method 2:
0054 print(df['Births'].max())
0055 
0056 # Create graph
0057 df['Births'].plot()
0058 
0059 # Maximum value in the data set
0060 MaxValue = df['Births'].max()
0061 
0062 # Name associated with the maximum value
0063 MaxName = df['Names'][df['Births'] == df['Births'].max()].values
0064 
0065 # Text to display on graph
0066 Text = str(MaxValue) + " - " + MaxName
0067 
0068 # Add text to graph
0069 pyplot.annotate(Text, xy=(1, MaxValue), xytext=(8, 0), 
0070                  xycoords=('axes fraction', 'data'), textcoords='offset points')
0071 
0072 print("The most popular name")
0073 print(df[df['Births'] == df['Births'].max()])
0074 
0075 
0076