#! /usr/bin/env python3 # def series_test ( ): import pandas as pd print ( '' ) print ( 'series_test():' ) print ( ' Demonstrate the use of Series in pandas.' ) # # Create a Series from a list. # print ( '' ) print ( ' Create a series:' ) print ( ' river_lengths = pd.Series ( [ 6300, 6650, 6275, 6400 ] )' ) river_lengths = pd.Series ( [ 6300, 6650, 6275, 6400 ] ) print ( river_lengths ) input ( 'RETURN') # # Print # print ( '' ) print ( ' Print with a loop "for i in range ( 0, 4 ):"' ) for i in range ( 0, 4 ): print ( river_lengths[i] ) input ( 'RETURN') # # Print with a generator. # print ( '' ) print ( ' Print with a generator "for length in river_lengths:"' ) for length in river_lengths: print ( length ) input ( 'RETURN') # # The index is an integer array by default. # print ( '' ) print ( ' Print river_lengths.index:' ) print ( river_lengths.index ) input ( 'RETURN') # # Specify a better index. # print ( '' ) print ( ' Specify river names as the index, and print river_lengths:' ) print ( " river_lengths = pd.Series ( [ 6300, 6650, 6275, 6400 ]," ) print ( " index = [ 'Yangtze', 'Nile', 'Mississippi', 'Amazon' ] )" ) river_lengths = pd.Series ( [ 6300, 6650, 6275, 6400 ], index = [ 'Yangtze', 'Nile', 'Mississippi', 'Amazon' ] ) print ( river_lengths ) input ( 'RETURN') # # Use the new index. # print ( '' ) print ( ' Use a river name to index a river length:' ) print ( " river_lengths['Nile'] = ", river_lengths['Nile'] ) input ( 'RETURN') # # Specify index, name and dtype. # print ( '' ) print ( ' Specify index, name and dtype.' ) print ( " river_lengths = pd.Series ( [ 6300, 6650, 6275, 6400 ]," ) print ( " index = [ 'Yangtze', 'Nile', 'Mississippi', 'Amazon' ]," ) print ( " name = 'Length (km)', dtype = float )" ) river_lengths = pd.Series ( [6300, 6650, 6275, 6400 ], index = [ 'Yangtze', 'Nile', 'Mississippi', 'Amazon' ], name = 'Length (km)', dtype = float ) print ( '' ) print ( ' print river_lengths' ) print ( river_lengths ) print ( '' ) print ( ' print river_lengths["Nile"]' ) print ( river_lengths['Nile'] ) print ( '' ) print ( ' print river_lengths["Yangtze":"Mississippi"]' ) print ( river_lengths['Yangtze':'Mississippi'] ) input ( 'RETURN') # # Filter the data. # print ( '' ) print ( ' Filter the data.' ) print ( ' print ( river_lengths[river_lengths < 6500] ):' ) print ( river_lengths[river_lengths < 6500] ) input ( 'RETURN') # # Convert to miles. # river_lengths_miles = river_lengths * 0.621371 river_lengths_miles.name = 'Length (miles)' print ( '' ) print ( ' Make a new Series, "river_lengths_miles"' ) print ( ' river_lengths_miles = river_lengths * 0.621371' ) print ( ' river_lengths_miles.name = "Length (miles)"' ) print ( river_lengths_miles ) input ( 'RETURN') # # Create two series from dicts. # Then create a third series by combining these two. # print ( '' ) print ( ' Series for mass of astronomical bodies:' ) print ( " mass = pd.Series ( { " ) print ( " 'Ganymede' : 1.482e23, " ) print ( " 'Callisto' : 1.076323, " ) print ( " 'Io' : 8.932e22, " ) print ( " 'Europa' : 4.800e22, " ) print ( " 'Moon' : 7.342e22, " ) print ( " 'Earth' : 5.972e24 }," ) print ( " name = 'mass (kg)' )" ) mass = pd.Series ( { \ 'Ganymede' : 1.482e23, \ 'Callisto' : 1.076323, \ 'Io' : 8.932e22, \ 'Europa' : 4.800e22, \ 'Moon' : 7.342e22, \ 'Earth' : 5.972e24 }, name = 'mass (kg)' ) print ( mass ) print ( '' ) print ( ' Series for radius of astronomical bodies:' ) print ( " radius = pd.Series ( { " ) print ( " 'Ganymede' : 2.634e6, " ) print ( " 'Io' : 1.822e6, " ) print ( " 'Moon' : 1.737e6, " ) print ( " 'Earth' : 6.371e6 }," ) print ( " name = 'radius (m)' )" ) radius = pd.Series ( { \ 'Ganymede' : 2.634e6, \ 'Io' : 1.822e6, \ 'Moon' : 1.737e6, \ 'Earth' : 6.371e6 }, name = 'radius (m)' ) print ( radius ) input ( 'RETURN') # # Create new series. # print ( '' ) print ( ' Create new series "gravity = G * mass / radius**2"' ) from scipy.constants import G gravity = G * mass / radius**2 gravity.name = 'surface gravity m/s**2' gravity.index.name = 'Body' print ( '' ) print ( gravity ) print ( '' ) print ( ' gravity.isnull() checks for NaN values.' ) print ( gravity.isnull() ) print ( '' ) print ( ' gravity = gravity.dropna() drops NaN entries.' ) gravity = gravity.dropna() print ( gravity ) # # Extract values into numpy array. # print ( '' ) print ( ' gravity_array = gravity.values creates numpy array.' ) gravity_array = gravity.values print ( gravity_array ) return if ( __name__ == "__main__" ): series_test ( )