Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-25 02:29:52

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