2. Logistics
• Please paste your repl link for this session in the google sheet
• Be prepared to share your screen
• A repl link with questions to all exercises we will do today in the
class will be provided
3. Measuring Run Time of Code
import time
start_time = time.time()
//
print(“Hello”)
//
seconds = time.time() - start_time
print('Time Taken:', time.strftime("%H:%M:%S", time.gmtime(seconds)))
# Output: Time Taken: 00:00:08
● Main() is the function that contains the code to be executed
● We use the time library to ease with measuring run time, various
time zones, and more
4. Dictionaries - Recap
• A dictionary consists of two things (a) keys (b) values
• Use strings to represent keys
• Values can be anything
5. Dictionaries - Recap
• Print a value in a dictionary
• Delete a value in a dictionary
• Print all keys of a dictionary
• Add values to a dictionary
6. Functions can accept and return multiple
values
• How would you call this function?
15. Interpreting CSV Data - Properties
• len() - Returns the total amount of rows
• shape() - Returns an object which contains the total number of rows and
columns
• head(n) - Retrieves the top n (Integer) rows
• info() - Displays all columns and their data types
• dtypes() - Retrieves the column title and its respective data type
• Columns() – Retrieves the column names
16. Pandas- Methods
• Dropping columns from a dataframe
Exercise: Print the new dataframe and check if the columns were dropped
17. Pandas- Methods
• Creating a dataframe from scratch
Note: This is useful when you want to create a
dataframe and add data to it later
19. Pandas- Exercise
• Create an empty dataframe with the following columns
• [`num_1`, `num_2`, `num_3`]
• Generate random numbers and add 10 rows to the dataframe
20. Sorting CSV Files - Methods
• Multiple different methods to sort columns and values
• sort_values() - sorting the DataFrame by one or more columns
• sort_index() - sorting the DataFrame by the row index
import pandas
nbaDataFrame = pd.read_csv("NBA_CSV_DATA.csv")
nbaDataFrame.sort_values(parameters)
nbaDataFrame.sort_index(parameters)
21. Exploring CSV File Data - Value
• Sorting columns by given player weight (decreasing to increasing)
import pandas
nbaDataFrame = pd.read_csv("NBA_CSV_DATA.csv")
sortedDataFrame = nbaDataFrame.sort_values('Weight',
ascending=True)
print sortedDataFrame[['Weight', 'Name']]
22. Exploring CSV File Data Output
Note* : Values are
sorted by row
index when values
are equal for
given sorting
factor.
23. Adding Elements to CSV File
• Create new data and append (add) to current CSV File
• Data is added to the end (tail) of the DataFrame
• We can use lists!
• If no value is given for a column, it is empty
24. Adding Elements to CSV File - String Concept
firstName = "Ray"
lastName = "Allen"
fullName = firstName + " " + lastName
print(fullName)
#Output:
# Ray Allen
• We can now think about this in terms of DataFrames!
25. Adding Elements to CSV File
• Creating a new DataFrame, without reading a new CSV File
dataFrame = pd.DataFrame([[Data]], columns=[Columns])
• Data and Columns are just lists!
26. Constructing our new Data Frame
• We want to add a new player (new data) to our NBA CSV file
(existing data)
Ex:
new_player_columns = ['Name', 'Team', 'Number', 'Position', 'Age',
'Height', 'Weight', 'College', 'Salary']
new_player_data = ['Ray Allen', 'Boston Celtics', 10, "C", 24, "6-
6", 190, "Boston College", 800000]
27. Creating our new Data Frame
• Now we can make our new DataFrame using the data we made
newPlayerDataFrame = pd.DataFrame([new_player_data], columns= new_player_columns)
28. Combining DataFrames Together
• concat(parameters) - Takes a list of DataFrames and combines them,
we can pass in various parameters
combinedDataFrame = pd.concat([nbaDataFrame, new_player_dataframe])
print(combinedDataFrame.tail())
*Note - We print the tail as data is added to the end.