140
CSci 127: Introduction to Computer Science hunter.cuny.edu/csci CSci 127 (Hunter) Lecture 6 Summer 2020 1 / 35

CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

CSci 127: Introduction to Computer Science

hunter.cuny.edu/csci

CSci 127 (Hunter) Lecture 6 Summer 2020 1 / 35

Page 2: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Frequently Asked Questions

From previous semesters.

Could you spend more time on circuits/logical expressions/truth tables/decisions?We will start with that today, and more in the following weeks.

I still don’t get indices and the brackets. Could you spend more time on that?Yes, we will, since1) it’s fundamental, and2) the same ideas are used for accessing formatted data (today’s topic).

I still don’t get what is meant by input?Input is data provided to a program each time it runs, it may change at each run.In this course we have used the input() function.

Should I have received email for this course?Absolutely!!! We often send important communication by email. If you have notbeen receiving email from us weekly, please check your spam folder.

CSci 127 (Hunter) Lecture 6 Summer 2020 2 / 35

Page 3: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Frequently Asked Questions

From previous semesters.

Could you spend more time on circuits/logical expressions/truth tables/decisions?

We will start with that today, and more in the following weeks.

I still don’t get indices and the brackets. Could you spend more time on that?Yes, we will, since1) it’s fundamental, and2) the same ideas are used for accessing formatted data (today’s topic).

I still don’t get what is meant by input?Input is data provided to a program each time it runs, it may change at each run.In this course we have used the input() function.

Should I have received email for this course?Absolutely!!! We often send important communication by email. If you have notbeen receiving email from us weekly, please check your spam folder.

CSci 127 (Hunter) Lecture 6 Summer 2020 2 / 35

Page 4: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Frequently Asked Questions

From previous semesters.

Could you spend more time on circuits/logical expressions/truth tables/decisions?We will start with that today, and more in the following weeks.

I still don’t get indices and the brackets. Could you spend more time on that?Yes, we will, since1) it’s fundamental, and2) the same ideas are used for accessing formatted data (today’s topic).

I still don’t get what is meant by input?Input is data provided to a program each time it runs, it may change at each run.In this course we have used the input() function.

Should I have received email for this course?Absolutely!!! We often send important communication by email. If you have notbeen receiving email from us weekly, please check your spam folder.

CSci 127 (Hunter) Lecture 6 Summer 2020 2 / 35

Page 5: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Frequently Asked Questions

From previous semesters.

Could you spend more time on circuits/logical expressions/truth tables/decisions?We will start with that today, and more in the following weeks.

I still don’t get indices and the brackets. Could you spend more time on that?

Yes, we will, since1) it’s fundamental, and2) the same ideas are used for accessing formatted data (today’s topic).

I still don’t get what is meant by input?Input is data provided to a program each time it runs, it may change at each run.In this course we have used the input() function.

Should I have received email for this course?Absolutely!!! We often send important communication by email. If you have notbeen receiving email from us weekly, please check your spam folder.

CSci 127 (Hunter) Lecture 6 Summer 2020 2 / 35

Page 6: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Frequently Asked Questions

From previous semesters.

Could you spend more time on circuits/logical expressions/truth tables/decisions?We will start with that today, and more in the following weeks.

I still don’t get indices and the brackets. Could you spend more time on that?Yes, we will, since1) it’s fundamental, and2) the same ideas are used for accessing formatted data (today’s topic).

I still don’t get what is meant by input?Input is data provided to a program each time it runs, it may change at each run.In this course we have used the input() function.

Should I have received email for this course?Absolutely!!! We often send important communication by email. If you have notbeen receiving email from us weekly, please check your spam folder.

CSci 127 (Hunter) Lecture 6 Summer 2020 2 / 35

Page 7: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Frequently Asked Questions

From previous semesters.

Could you spend more time on circuits/logical expressions/truth tables/decisions?We will start with that today, and more in the following weeks.

I still don’t get indices and the brackets. Could you spend more time on that?Yes, we will, since1) it’s fundamental, and2) the same ideas are used for accessing formatted data (today’s topic).

I still don’t get what is meant by input?

Input is data provided to a program each time it runs, it may change at each run.In this course we have used the input() function.

Should I have received email for this course?Absolutely!!! We often send important communication by email. If you have notbeen receiving email from us weekly, please check your spam folder.

CSci 127 (Hunter) Lecture 6 Summer 2020 2 / 35

Page 8: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Frequently Asked Questions

From previous semesters.

Could you spend more time on circuits/logical expressions/truth tables/decisions?We will start with that today, and more in the following weeks.

I still don’t get indices and the brackets. Could you spend more time on that?Yes, we will, since1) it’s fundamental, and2) the same ideas are used for accessing formatted data (today’s topic).

I still don’t get what is meant by input?Input is data provided to a program each time it runs, it may change at each run.In this course we have used the input() function.

Should I have received email for this course?Absolutely!!! We often send important communication by email. If you have notbeen receiving email from us weekly, please check your spam folder.

CSci 127 (Hunter) Lecture 6 Summer 2020 2 / 35

Page 9: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Frequently Asked Questions

From previous semesters.

Could you spend more time on circuits/logical expressions/truth tables/decisions?We will start with that today, and more in the following weeks.

I still don’t get indices and the brackets. Could you spend more time on that?Yes, we will, since1) it’s fundamental, and2) the same ideas are used for accessing formatted data (today’s topic).

I still don’t get what is meant by input?Input is data provided to a program each time it runs, it may change at each run.In this course we have used the input() function.

Should I have received email for this course?

Absolutely!!! We often send important communication by email. If you have notbeen receiving email from us weekly, please check your spam folder.

CSci 127 (Hunter) Lecture 6 Summer 2020 2 / 35

Page 10: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Frequently Asked Questions

From previous semesters.

Could you spend more time on circuits/logical expressions/truth tables/decisions?We will start with that today, and more in the following weeks.

I still don’t get indices and the brackets. Could you spend more time on that?Yes, we will, since1) it’s fundamental, and2) the same ideas are used for accessing formatted data (today’s topic).

I still don’t get what is meant by input?Input is data provided to a program each time it runs, it may change at each run.In this course we have used the input() function.

Should I have received email for this course?Absolutely!!! We often send important communication by email. If you have notbeen receiving email from us weekly, please check your spam folder.

CSci 127 (Hunter) Lecture 6 Summer 2020 2 / 35

Page 11: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Today’s Topics

Recap: Logical Expressions & Circuits

Design: Cropping Images

Accessing Formatted Data

Design Challenge: Astrophysics andastropy

CSci 127 (Hunter) Lecture 6 Summer 2020 3 / 35

Page 12: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Today’s Topics

Recap: Logical Expressions & Circuits

Design: Cropping Images

Accessing Formatted Data

Design Challenge: Astrophysics andastropy

CSci 127 (Hunter) Lecture 6 Summer 2020 4 / 35

Page 13: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Recap: Logical Operators

and

in1 in2 returns:False and False False

False and True False

True and False False

True and True True

or

in1 in2 returns:False or False False

False or True True

True or False True

True or True True

not

in1 returns:not False True

not True False

CSci 127 (Hunter) Lecture 6 Summer 2020 5 / 35

Page 14: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Recap: Logical Operators

and

in1 in2 returns:False and False False

False and True False

True and False False

True and True True

or

in1 in2 returns:False or False False

False or True True

True or False True

True or True True

not

in1 returns:not False True

not True False

CSci 127 (Hunter) Lecture 6 Summer 2020 5 / 35

Page 15: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Recap: Logical Operators

and

in1 in2 returns:False and False False

False and True False

True and False False

True and True True

or

in1 in2 returns:False or False False

False or True True

True or False True

True or True True

not

in1 returns:not False True

not True False

CSci 127 (Hunter) Lecture 6 Summer 2020 5 / 35

Page 16: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Logical Operators & Circuits

Each logical operator (and, or, &not) can be used to join togetherexpressions.

Example: in1 and in2

Each logical operator (and, or, &not) has a corresponding logicalcircuit that can be used to jointogether inputs.

CSci 127 (Hunter) Lecture 6 Summer 2020 6 / 35

Page 17: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Logical Operators & Circuits

Each logical operator (and, or, &not) can be used to join togetherexpressions.

Example: in1 and in2

Each logical operator (and, or, &not) has a corresponding logicalcircuit that can be used to jointogether inputs.

CSci 127 (Hunter) Lecture 6 Summer 2020 6 / 35

Page 18: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Logical Operators & Circuits

Each logical operator (and, or, &not) can be used to join togetherexpressions.

Example: in1 and in2

Each logical operator (and, or, &not) has a corresponding logicalcircuit that can be used to jointogether inputs.

CSci 127 (Hunter) Lecture 6 Summer 2020 6 / 35

Page 19: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Logical Operators & Circuits

Each logical operator (and, or, &not) can be used to join togetherexpressions.

Example: in1 and in2

Each logical operator (and, or, &not) has a corresponding logicalcircuit that can be used to jointogether inputs.

CSci 127 (Hunter) Lecture 6 Summer 2020 6 / 35

Page 20: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Examples: Logical Circuit

(in1 and in2) and in3

CSci 127 (Hunter) Lecture 6 Summer 2020 7 / 35

Page 21: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Examples: Logical Circuit

(in1 and in2) and in3

CSci 127 (Hunter) Lecture 6 Summer 2020 7 / 35

Page 22: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

More Circuit Examples

Examples from last lecture:

Draw a circuit that corresponds to each logical expression:

not( in1 or in2 )

(in1 or in2) and (in1 or in3)

(not(in1 and not in2)) or (in1 and (in2 and in3))

CSci 127 (Hunter) Lecture 6 Summer 2020 8 / 35

Page 23: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem:

Predict what the code will do:

CSci 127 (Hunter) Lecture 6 Summer 2020 9 / 35

Page 24: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Python Tutor

(Demo with pythonTutor)

CSci 127 (Hunter) Lecture 6 Summer 2020 10 / 35

Page 25: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Today’s Topics

Recap: Logical Expressions & Circuits

Design: Cropping Images

Accessing Formatted Data

Design Challenge: Astrophysics andastropy

CSci 127 (Hunter) Lecture 6 Summer 2020 11 / 35

Page 26: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem: Design Question

From Final Exam, Fall 2017, V4, #6.

Design an algorithm that reads in an image and displays the lower leftcorner of the image.

Input:Output:Process: (Brainstorm for a “To Do” list to accomplish this.)

CSci 127 (Hunter) Lecture 6 Summer 2020 12 / 35

Page 27: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem: Design Question

From Final Exam, Fall 2017, V4, #6.

Design an algorithm that reads in an image and displays the lower leftcorner of the image.

Input:Output:Process: (Brainstorm for a “To Do” list to accomplish this.)

CSci 127 (Hunter) Lecture 6 Summer 2020 12 / 35

Page 28: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Design Question

Design a program that asks the user for an image and then display theupper left quarter of the image. (First, design the pseudocode, and if time,expand to a Python program.)

How to approach this:

Create a “To Do” list of what your program has to accomplish.

Read through the problem, and break it into “To Do” items.

Don’t worry if you don’t know how to do all the items you write down.

Example:

1 Import libraries.2 Ask user for an image name.3 Read in image.4 Figure out size of image.5 Make a new image that’s half the height and half the width.6 Display the new image.

CSci 127 (Hunter) Lecture 6 Summer 2020 13 / 35

Page 29: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Design Question

Design a program that asks the user for an image and then display theupper left quarter of the image. (First, design the pseudocode, and if time,expand to a Python program.)

How to approach this:

Create a “To Do” list of what your program has to accomplish.

Read through the problem, and break it into “To Do” items.

Don’t worry if you don’t know how to do all the items you write down.

Example:

1 Import libraries.2 Ask user for an image name.3 Read in image.4 Figure out size of image.5 Make a new image that’s half the height and half the width.6 Display the new image.

CSci 127 (Hunter) Lecture 6 Summer 2020 13 / 35

Page 30: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Design Question

Design a program that asks the user for an image and then display theupper left quarter of the image. (First, design the pseudocode, and if time,expand to a Python program.)

How to approach this:

Create a “To Do” list of what your program has to accomplish.

Read through the problem, and break it into “To Do” items.

Don’t worry if you don’t know how to do all the items you write down.

Example:

1 Import libraries.2 Ask user for an image name.3 Read in image.4 Figure out size of image.5 Make a new image that’s half the height and half the width.6 Display the new image.

CSci 127 (Hunter) Lecture 6 Summer 2020 13 / 35

Page 31: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Design Question

Design a program that asks the user for an image and then display theupper left quarter of the image. (First, design the pseudocode, and if time,expand to a Python program.)

How to approach this:

Create a “To Do” list of what your program has to accomplish.

Read through the problem, and break it into “To Do” items.

Don’t worry if you don’t know how to do all the items you write down.

Example:

1 Import libraries.2 Ask user for an image name.3 Read in image.4 Figure out size of image.5 Make a new image that’s half the height and half the width.6 Display the new image.

CSci 127 (Hunter) Lecture 6 Summer 2020 13 / 35

Page 32: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Design Question

Design a program that asks the user for an image and then display theupper left quarter of the image. (First, design the pseudocode, and if time,expand to a Python program.)

How to approach this:

Create a “To Do” list of what your program has to accomplish.

Read through the problem, and break it into “To Do” items.

Don’t worry if you don’t know how to do all the items you write down.

Example:

1 Import libraries.2 Ask user for an image name.3 Read in image.4 Figure out size of image.5 Make a new image that’s half the height and half the width.6 Display the new image.

CSci 127 (Hunter) Lecture 6 Summer 2020 13 / 35

Page 33: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Design Question

Design a program that asks the user for an image and then display theupper left quarter of the image. (First, design the pseudocode, and if time,expand to a Python program.)

How to approach this:

Create a “To Do” list of what your program has to accomplish.

Read through the problem, and break it into “To Do” items.

Don’t worry if you don’t know how to do all the items you write down.

Example:

1 Import libraries.

2 Ask user for an image name.3 Read in image.4 Figure out size of image.5 Make a new image that’s half the height and half the width.6 Display the new image.

CSci 127 (Hunter) Lecture 6 Summer 2020 13 / 35

Page 34: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Design Question

Design a program that asks the user for an image and then display theupper left quarter of the image. (First, design the pseudocode, and if time,expand to a Python program.)

How to approach this:

Create a “To Do” list of what your program has to accomplish.

Read through the problem, and break it into “To Do” items.

Don’t worry if you don’t know how to do all the items you write down.

Example:

1 Import libraries.2 Ask user for an image name.

3 Read in image.4 Figure out size of image.5 Make a new image that’s half the height and half the width.6 Display the new image.

CSci 127 (Hunter) Lecture 6 Summer 2020 13 / 35

Page 35: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Design Question

Design a program that asks the user for an image and then display theupper left quarter of the image. (First, design the pseudocode, and if time,expand to a Python program.)

How to approach this:

Create a “To Do” list of what your program has to accomplish.

Read through the problem, and break it into “To Do” items.

Don’t worry if you don’t know how to do all the items you write down.

Example:

1 Import libraries.2 Ask user for an image name.3 Read in image.

4 Figure out size of image.5 Make a new image that’s half the height and half the width.6 Display the new image.

CSci 127 (Hunter) Lecture 6 Summer 2020 13 / 35

Page 36: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Design Question

Design a program that asks the user for an image and then display theupper left quarter of the image. (First, design the pseudocode, and if time,expand to a Python program.)

How to approach this:

Create a “To Do” list of what your program has to accomplish.

Read through the problem, and break it into “To Do” items.

Don’t worry if you don’t know how to do all the items you write down.

Example:

1 Import libraries.2 Ask user for an image name.3 Read in image.4 Figure out size of image.

5 Make a new image that’s half the height and half the width.6 Display the new image.

CSci 127 (Hunter) Lecture 6 Summer 2020 13 / 35

Page 37: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Design Question

Design a program that asks the user for an image and then display theupper left quarter of the image. (First, design the pseudocode, and if time,expand to a Python program.)

How to approach this:

Create a “To Do” list of what your program has to accomplish.

Read through the problem, and break it into “To Do” items.

Don’t worry if you don’t know how to do all the items you write down.

Example:

1 Import libraries.2 Ask user for an image name.3 Read in image.4 Figure out size of image.5 Make a new image that’s half the height and half the width.

6 Display the new image.

CSci 127 (Hunter) Lecture 6 Summer 2020 13 / 35

Page 38: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Design Question

Design a program that asks the user for an image and then display theupper left quarter of the image. (First, design the pseudocode, and if time,expand to a Python program.)

How to approach this:

Create a “To Do” list of what your program has to accomplish.

Read through the problem, and break it into “To Do” items.

Don’t worry if you don’t know how to do all the items you write down.

Example:

1 Import libraries.2 Ask user for an image name.3 Read in image.4 Figure out size of image.5 Make a new image that’s half the height and half the width.6 Display the new image.

CSci 127 (Hunter) Lecture 6 Summer 2020 13 / 35

Page 39: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem: Design Question

1 Import libraries.

import matplotlib.pyplot as plt

import numpy as np

2 Ask user for an image name.

inF = input(’Enter file name: ’)

3 Read in image.

img = plt.imread(inF) #Read in image from inF

4 Figure out size of image.

height = img.shape[0] #Get height

width = img.shape[1] #Get width

5 Make a new image that’s half the height and half the width.img2 = img[height//2:, :width//2] #Crop to lower left corner

6 Display the new image.

plt.imshow(img2) #Load our new image into pyplot

plt.show() #Show the image (waits until closed to continue)

CSci 127 (Hunter) Lecture 6 Summer 2020 14 / 35

Page 40: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem: Design Question

1 Import libraries.

import matplotlib.pyplot as plt

import numpy as np

2 Ask user for an image name.

inF = input(’Enter file name: ’)

3 Read in image.

img = plt.imread(inF) #Read in image from inF

4 Figure out size of image.

height = img.shape[0] #Get height

width = img.shape[1] #Get width

5 Make a new image that’s half the height and half the width.img2 = img[height//2:, :width//2] #Crop to lower left corner

6 Display the new image.

plt.imshow(img2) #Load our new image into pyplot

plt.show() #Show the image (waits until closed to continue)

CSci 127 (Hunter) Lecture 6 Summer 2020 14 / 35

Page 41: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem: Design Question

1 Import libraries.

import matplotlib.pyplot as plt

import numpy as np

2 Ask user for an image name.

inF = input(’Enter file name: ’)

3 Read in image.

img = plt.imread(inF) #Read in image from inF

4 Figure out size of image.

height = img.shape[0] #Get height

width = img.shape[1] #Get width

5 Make a new image that’s half the height and half the width.img2 = img[height//2:, :width//2] #Crop to lower left corner

6 Display the new image.

plt.imshow(img2) #Load our new image into pyplot

plt.show() #Show the image (waits until closed to continue)

CSci 127 (Hunter) Lecture 6 Summer 2020 14 / 35

Page 42: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem: Design Question

1 Import libraries.

import matplotlib.pyplot as plt

import numpy as np

2 Ask user for an image name.

inF = input(’Enter file name: ’)

3 Read in image.

img = plt.imread(inF) #Read in image from inF

4 Figure out size of image.

height = img.shape[0] #Get height

width = img.shape[1] #Get width

5 Make a new image that’s half the height and half the width.img2 = img[height//2:, :width//2] #Crop to lower left corner

6 Display the new image.

plt.imshow(img2) #Load our new image into pyplot

plt.show() #Show the image (waits until closed to continue)

CSci 127 (Hunter) Lecture 6 Summer 2020 14 / 35

Page 43: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem: Design Question

1 Import libraries.

import matplotlib.pyplot as plt

import numpy as np

2 Ask user for an image name.

inF = input(’Enter file name: ’)

3 Read in image.

img = plt.imread(inF) #Read in image from inF

4 Figure out size of image.

height = img.shape[0] #Get height

width = img.shape[1] #Get width

5 Make a new image that’s half the height and half the width.img2 = img[height//2:, :width//2] #Crop to lower left corner

6 Display the new image.

plt.imshow(img2) #Load our new image into pyplot

plt.show() #Show the image (waits until closed to continue)

CSci 127 (Hunter) Lecture 6 Summer 2020 14 / 35

Page 44: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem: Design Question

1 Import libraries.

import matplotlib.pyplot as plt

import numpy as np

2 Ask user for an image name.

inF = input(’Enter file name: ’)

3 Read in image.

img = plt.imread(inF) #Read in image from inF

4 Figure out size of image.

height = img.shape[0] #Get height

width = img.shape[1] #Get width

5 Make a new image that’s half the height and half the width.img2 = img[height//2:, :width//2] #Crop to lower left corner

6 Display the new image.

plt.imshow(img2) #Load our new image into pyplot

plt.show() #Show the image (waits until closed to continue)

CSci 127 (Hunter) Lecture 6 Summer 2020 14 / 35

Page 45: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem: Design Question

1 Import libraries.

import matplotlib.pyplot as plt

import numpy as np

2 Ask user for an image name.

inF = input(’Enter file name: ’)

3 Read in image.

img = plt.imread(inF) #Read in image from inF

4 Figure out size of image.

height = img.shape[0] #Get height

width = img.shape[1] #Get width

5 Make a new image that’s half the height and half the width.img2 = img[height//2:, :width//2] #Crop to lower left corner

6 Display the new image.

plt.imshow(img2) #Load our new image into pyplot

plt.show() #Show the image (waits until closed to continue)

CSci 127 (Hunter) Lecture 6 Summer 2020 14 / 35

Page 46: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem: Design Question

1 Import libraries.

import matplotlib.pyplot as plt

import numpy as np

2 Ask user for an image name.

inF = input(’Enter file name: ’)

3 Read in image.

img = plt.imread(inF) #Read in image from inF

4 Figure out size of image.

height = img.shape[0] #Get height

width = img.shape[1] #Get width

5 Make a new image that’s half the height and half the width.img2 = img[height//2:, :width//2] #Crop to lower left corner

6 Display the new image.

plt.imshow(img2) #Load our new image into pyplot

plt.show() #Show the image (waits until closed to continue)

CSci 127 (Hunter) Lecture 6 Summer 2020 14 / 35

Page 47: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem: Design Question

1 Import libraries.

import matplotlib.pyplot as plt

import numpy as np

2 Ask user for an image name.

inF = input(’Enter file name: ’)

3 Read in image.

img = plt.imread(inF) #Read in image from inF

4 Figure out size of image.

height = img.shape[0] #Get height

width = img.shape[1] #Get width

5 Make a new image that’s half the height and half the width.

img2 = img[height//2:, :width//2] #Crop to lower left corner

6 Display the new image.

plt.imshow(img2) #Load our new image into pyplot

plt.show() #Show the image (waits until closed to continue)

CSci 127 (Hunter) Lecture 6 Summer 2020 14 / 35

Page 48: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem: Design Question

1 Import libraries.

import matplotlib.pyplot as plt

import numpy as np

2 Ask user for an image name.

inF = input(’Enter file name: ’)

3 Read in image.

img = plt.imread(inF) #Read in image from inF

4 Figure out size of image.

height = img.shape[0] #Get height

width = img.shape[1] #Get width

5 Make a new image that’s half the height and half the width.img2 = img[height//2:, :width//2] #Crop to lower left corner

6 Display the new image.

plt.imshow(img2) #Load our new image into pyplot

plt.show() #Show the image (waits until closed to continue)

CSci 127 (Hunter) Lecture 6 Summer 2020 14 / 35

Page 49: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem: Design Question

1 Import libraries.

import matplotlib.pyplot as plt

import numpy as np

2 Ask user for an image name.

inF = input(’Enter file name: ’)

3 Read in image.

img = plt.imread(inF) #Read in image from inF

4 Figure out size of image.

height = img.shape[0] #Get height

width = img.shape[1] #Get width

5 Make a new image that’s half the height and half the width.img2 = img[height//2:, :width//2] #Crop to lower left corner

6 Display the new image.

plt.imshow(img2) #Load our new image into pyplot

plt.show() #Show the image (waits until closed to continue)

CSci 127 (Hunter) Lecture 6 Summer 2020 14 / 35

Page 50: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Today’s Topics

Recap: Logical Expressions & Circuits

Design: Cropping Images

Accessing Formatted Data

Design Challenge: Astrophysics andastropy

CSci 127 (Hunter) Lecture 6 Summer 2020 15 / 35

Page 51: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Structured Data

Common to have data structured in a spread sheet.

In the example above, we have the first line that says“Undergraduate”.

Next line has the titles for the columns.

Subsequent lines have a college and attributes about the college.

Python has several ways to read in such data.

We will use the popular Python Data Analysis Library (Pandas).

CSci 127 (Hunter) Lecture 6 Summer 2020 16 / 35

Page 52: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Structured Data

Common to have data structured in a spread sheet.

In the example above, we have the first line that says“Undergraduate”.

Next line has the titles for the columns.

Subsequent lines have a college and attributes about the college.

Python has several ways to read in such data.

We will use the popular Python Data Analysis Library (Pandas).

CSci 127 (Hunter) Lecture 6 Summer 2020 16 / 35

Page 53: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Structured Data

Common to have data structured in a spread sheet.

In the example above, we have the first line that says“Undergraduate”.

Next line has the titles for the columns.

Subsequent lines have a college and attributes about the college.

Python has several ways to read in such data.

We will use the popular Python Data Analysis Library (Pandas).

CSci 127 (Hunter) Lecture 6 Summer 2020 16 / 35

Page 54: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Structured Data

Common to have data structured in a spread sheet.

In the example above, we have the first line that says“Undergraduate”.

Next line has the titles for the columns.

Subsequent lines have a college and attributes about the college.

Python has several ways to read in such data.

We will use the popular Python Data Analysis Library (Pandas).

CSci 127 (Hunter) Lecture 6 Summer 2020 16 / 35

Page 55: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Structured Data

Common to have data structured in a spread sheet.

In the example above, we have the first line that says“Undergraduate”.

Next line has the titles for the columns.

Subsequent lines have a college and attributes about the college.

Python has several ways to read in such data.

We will use the popular Python Data Analysis Library (Pandas).

CSci 127 (Hunter) Lecture 6 Summer 2020 16 / 35

Page 56: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Structured Data

Common to have data structured in a spread sheet.

In the example above, we have the first line that says“Undergraduate”.

Next line has the titles for the columns.

Subsequent lines have a college and attributes about the college.

Python has several ways to read in such data.

We will use the popular Python Data Analysis Library (Pandas).

CSci 127 (Hunter) Lecture 6 Summer 2020 16 / 35

Page 57: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Structured Data

We will use the popular Python Data Analysis Library (Pandas).

Open source and freely available (part of anaconda distribution).

Already loaded on the machines in 1001E North.

See end of Lab 6 for directions on downloading it to your homemachine.

To use, add to the top of your file:

import pandas as pd

CSci 127 (Hunter) Lecture 6 Summer 2020 17 / 35

Page 58: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Structured Data

We will use the popular Python Data Analysis Library (Pandas).

Open source and freely available (part of anaconda distribution).

Already loaded on the machines in 1001E North.

See end of Lab 6 for directions on downloading it to your homemachine.

To use, add to the top of your file:

import pandas as pd

CSci 127 (Hunter) Lecture 6 Summer 2020 17 / 35

Page 59: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Structured Data

We will use the popular Python Data Analysis Library (Pandas).

Open source and freely available (part of anaconda distribution).

Already loaded on the machines in 1001E North.

See end of Lab 6 for directions on downloading it to your homemachine.

To use, add to the top of your file:

import pandas as pd

CSci 127 (Hunter) Lecture 6 Summer 2020 17 / 35

Page 60: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Structured Data

We will use the popular Python Data Analysis Library (Pandas).

Open source and freely available (part of anaconda distribution).

Already loaded on the machines in 1001E North.

See end of Lab 6 for directions on downloading it to your homemachine.

To use, add to the top of your file:

import pandas as pd

CSci 127 (Hunter) Lecture 6 Summer 2020 17 / 35

Page 61: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Structured Data

We will use the popular Python Data Analysis Library (Pandas).

Open source and freely available (part of anaconda distribution).

Already loaded on the machines in 1001E North.

See end of Lab 6 for directions on downloading it to your homemachine.

To use, add to the top of your file:

import pandas as pd

CSci 127 (Hunter) Lecture 6 Summer 2020 17 / 35

Page 62: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

CSV Files

Excel .xls files have much extra formatting.

The text file version is called CSV for comma separated values.

Each row is a line in the file.

Columns are separated by commas on each line.

CSci 127 (Hunter) Lecture 6 Summer 2020 18 / 35

Page 63: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

CSV Files

Excel .xls files have much extra formatting.

The text file version is called CSV for comma separated values.

Each row is a line in the file.

Columns are separated by commas on each line.

CSci 127 (Hunter) Lecture 6 Summer 2020 18 / 35

Page 64: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

CSV Files

Excel .xls files have much extra formatting.

The text file version is called CSV for comma separated values.

Each row is a line in the file.

Columns are separated by commas on each line.

CSci 127 (Hunter) Lecture 6 Summer 2020 18 / 35

Page 65: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

CSV Files

Excel .xls files have much extra formatting.

The text file version is called CSV for comma separated values.

Each row is a line in the file.

Columns are separated by commas on each line.

CSci 127 (Hunter) Lecture 6 Summer 2020 18 / 35

Page 66: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

CSV Files

nycHistPop.csv

CSci 127 (Hunter) Lecture 6 Summer 2020 19 / 35

Page 67: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Reading in CSV Files

To read in a CSV file: myVar = pd.read csv("myFile.csv")

Pandas has its own type, DataFrame, that is perfect for holding asheet of data.

Often abbreviated: df.

It also has Series, that is perfect for holding a row or column of data.

CSci 127 (Hunter) Lecture 6 Summer 2020 20 / 35

Page 68: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Reading in CSV Files

To read in a CSV file: myVar = pd.read csv("myFile.csv")

Pandas has its own type, DataFrame, that is perfect for holding asheet of data.

Often abbreviated: df.

It also has Series, that is perfect for holding a row or column of data.

CSci 127 (Hunter) Lecture 6 Summer 2020 20 / 35

Page 69: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Reading in CSV Files

To read in a CSV file: myVar = pd.read csv("myFile.csv")

Pandas has its own type, DataFrame, that is perfect for holding asheet of data.

Often abbreviated: df.

It also has Series, that is perfect for holding a row or column of data.

CSci 127 (Hunter) Lecture 6 Summer 2020 20 / 35

Page 70: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Reading in CSV Files

To read in a CSV file: myVar = pd.read csv("myFile.csv")

Pandas has its own type, DataFrame, that is perfect for holding asheet of data.

Often abbreviated: df.

It also has Series, that is perfect for holding a row or column of data.

CSci 127 (Hunter) Lecture 6 Summer 2020 20 / 35

Page 71: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Example: Reading in CSV Files

nycHistPop.csv

In Lab 6

import matplotlib.pyplot as plt

import pandas as pd

pop = pd.read csv(’nycHistPop.csv’,skiprows=5)

pop.plot(x="Year")

plt.show()

CSci 127 (Hunter) Lecture 6 Summer 2020 21 / 35

Page 72: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Example: Reading in CSV Files

nycHistPop.csv

In Lab 6

import matplotlib.pyplot as plt

import pandas as pd

pop = pd.read csv(’nycHistPop.csv’,skiprows=5)

pop.plot(x="Year")

plt.show()

CSci 127 (Hunter) Lecture 6 Summer 2020 21 / 35

Page 73: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Example: Reading in CSV Files

nycHistPop.csv

In Lab 6

import matplotlib.pyplot as plt

import pandas as pd

pop = pd.read csv(’nycHistPop.csv’,skiprows=5)

pop.plot(x="Year")

plt.show()

CSci 127 (Hunter) Lecture 6 Summer 2020 21 / 35

Page 74: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Example: Reading in CSV Files

nycHistPop.csv

In Lab 6

import matplotlib.pyplot as plt

import pandas as pd

pop = pd.read csv(’nycHistPop.csv’,skiprows=5)

pop.plot(x="Year")

plt.show()

CSci 127 (Hunter) Lecture 6 Summer 2020 21 / 35

Page 75: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Example: Reading in CSV Files

nycHistPop.csv

In Lab 6

import matplotlib.pyplot as plt

import pandas as pd

pop = pd.read csv(’nycHistPop.csv’,skiprows=5)

pop.plot(x="Year")

plt.show()

CSci 127 (Hunter) Lecture 6 Summer 2020 21 / 35

Page 76: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Series in Pandas

Series can store a column or row of a DataFrame.

Example: pop["Manhattan"] is the Series corresponding to the column ofManhattan data.

Example:print("The largest number living in the Bronx is",

pop["Bronx"].max())

CSci 127 (Hunter) Lecture 6 Summer 2020 22 / 35

Page 77: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Series in Pandas

Series can store a column or row of a DataFrame.

Example: pop["Manhattan"] is the Series corresponding to the column ofManhattan data.

Example:print("The largest number living in the Bronx is",

pop["Bronx"].max())

CSci 127 (Hunter) Lecture 6 Summer 2020 22 / 35

Page 78: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Series in Pandas

Series can store a column or row of a DataFrame.

Example: pop["Manhattan"] is the Series corresponding to the column ofManhattan data.

Example:print("The largest number living in the Bronx is",

pop["Bronx"].max())

CSci 127 (Hunter) Lecture 6 Summer 2020 22 / 35

Page 79: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem

Predict what the following will do:

print("Queens:", pop["Queens"].min())

print("S I:", pop["Staten Island"].mean())

print("S I:", pop["Staten Island"].std())

pop.plot.bar(x="Year")

pop.plot.scatter(x="Brooklyn", y= "Total")

pop["Fraction"] = pop["Bronx"]/pop["Total"]

CSci 127 (Hunter) Lecture 6 Summer 2020 23 / 35

Page 80: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem

Predict what the following will do:

print("Queens:", pop["Queens"].min())

print("S I:", pop["Staten Island"].mean())

print("S I:", pop["Staten Island"].std())

pop.plot.bar(x="Year")

pop.plot.scatter(x="Brooklyn", y= "Total")

pop["Fraction"] = pop["Bronx"]/pop["Total"]

CSci 127 (Hunter) Lecture 6 Summer 2020 23 / 35

Page 81: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem

Predict what the following will do:

print("Queens:", pop["Queens"].min())

print("S I:", pop["Staten Island"].mean())

print("S I:", pop["Staten Island"].std())

pop.plot.bar(x="Year")

pop.plot.scatter(x="Brooklyn", y= "Total")

pop["Fraction"] = pop["Bronx"]/pop["Total"]

CSci 127 (Hunter) Lecture 6 Summer 2020 23 / 35

Page 82: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem

Predict what the following will do:

print("Queens:", pop["Queens"].min())

print("S I:", pop["Staten Island"].mean())

print("S I:", pop["Staten Island"].std())

pop.plot.bar(x="Year")

pop.plot.scatter(x="Brooklyn", y= "Total")

pop["Fraction"] = pop["Bronx"]/pop["Total"]

CSci 127 (Hunter) Lecture 6 Summer 2020 23 / 35

Page 83: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem

Predict what the following will do:

print("Queens:", pop["Queens"].min())

print("S I:", pop["Staten Island"].mean())

print("S I:", pop["Staten Island"].std())

pop.plot.bar(x="Year")

pop.plot.scatter(x="Brooklyn", y= "Total")

pop["Fraction"] = pop["Bronx"]/pop["Total"]

CSci 127 (Hunter) Lecture 6 Summer 2020 23 / 35

Page 84: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem

Predict what the following will do:

print("Queens:", pop["Queens"].min())

print("S I:", pop["Staten Island"].mean())

print("S I:", pop["Staten Island"].std())

pop.plot.bar(x="Year")

pop.plot.scatter(x="Brooklyn", y= "Total")

pop["Fraction"] = pop["Bronx"]/pop["Total"]

CSci 127 (Hunter) Lecture 6 Summer 2020 23 / 35

Page 85: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Solutions

Predict what the following will do:

print("Queens:", pop["Queens"].min())

Minimum value in the column with label “Queens”.

print("S I:", pop["Staten Island"].mean())

Average of values in the column “Staten Island”.

print("S I :", pop["Staten Island"].std())

Standard deviation of values in the column “StatenIsland”.

pop.plot.bar(x="Year")

Bar chart with x-axis ”Year”.

pop.plot.scatter(x="Brooklyn", y= "Total")

Scatter plot of Brooklyn versus Total values.

pop["Fraction"] = pop["Bronx"]/pop["Total"]

New column with the fraction of population thatlives in the Bronx.

CSci 127 (Hunter) Lecture 6 Summer 2020 24 / 35

Page 86: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Solutions

Predict what the following will do:

print("Queens:", pop["Queens"].min())

Minimum value in the column with label “Queens”.

print("S I:", pop["Staten Island"].mean())

Average of values in the column “Staten Island”.

print("S I :", pop["Staten Island"].std())

Standard deviation of values in the column “StatenIsland”.

pop.plot.bar(x="Year")

Bar chart with x-axis ”Year”.

pop.plot.scatter(x="Brooklyn", y= "Total")

Scatter plot of Brooklyn versus Total values.

pop["Fraction"] = pop["Bronx"]/pop["Total"]

New column with the fraction of population thatlives in the Bronx.

CSci 127 (Hunter) Lecture 6 Summer 2020 24 / 35

Page 87: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Solutions

Predict what the following will do:

print("Queens:", pop["Queens"].min())

Minimum value in the column with label “Queens”.

print("S I:", pop["Staten Island"].mean())

Average of values in the column “Staten Island”.

print("S I :", pop["Staten Island"].std())

Standard deviation of values in the column “StatenIsland”.

pop.plot.bar(x="Year")

Bar chart with x-axis ”Year”.

pop.plot.scatter(x="Brooklyn", y= "Total")

Scatter plot of Brooklyn versus Total values.

pop["Fraction"] = pop["Bronx"]/pop["Total"]

New column with the fraction of population thatlives in the Bronx.

CSci 127 (Hunter) Lecture 6 Summer 2020 24 / 35

Page 88: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Solutions

Predict what the following will do:

print("Queens:", pop["Queens"].min())

Minimum value in the column with label “Queens”.

print("S I:", pop["Staten Island"].mean())

Average of values in the column “Staten Island”.

print("S I :", pop["Staten Island"].std())

Standard deviation of values in the column “StatenIsland”.

pop.plot.bar(x="Year")

Bar chart with x-axis ”Year”.

pop.plot.scatter(x="Brooklyn", y= "Total")

Scatter plot of Brooklyn versus Total values.

pop["Fraction"] = pop["Bronx"]/pop["Total"]

New column with the fraction of population thatlives in the Bronx.

CSci 127 (Hunter) Lecture 6 Summer 2020 24 / 35

Page 89: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Solutions

Predict what the following will do:

print("Queens:", pop["Queens"].min())

Minimum value in the column with label “Queens”.

print("S I:", pop["Staten Island"].mean())

Average of values in the column “Staten Island”.

print("S I :", pop["Staten Island"].std())

Standard deviation of values in the column “StatenIsland”.

pop.plot.bar(x="Year")

Bar chart with x-axis ”Year”.

pop.plot.scatter(x="Brooklyn", y= "Total")

Scatter plot of Brooklyn versus Total values.

pop["Fraction"] = pop["Bronx"]/pop["Total"]

New column with the fraction of population thatlives in the Bronx.

CSci 127 (Hunter) Lecture 6 Summer 2020 24 / 35

Page 90: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Solutions

Predict what the following will do:

print("Queens:", pop["Queens"].min())

Minimum value in the column with label “Queens”.

print("S I:", pop["Staten Island"].mean())

Average of values in the column “Staten Island”.

print("S I :", pop["Staten Island"].std())

Standard deviation of values in the column “StatenIsland”.

pop.plot.bar(x="Year")

Bar chart with x-axis ”Year”.

pop.plot.scatter(x="Brooklyn", y= "Total")

Scatter plot of Brooklyn versus Total values.

pop["Fraction"] = pop["Bronx"]/pop["Total"]

New column with the fraction of population thatlives in the Bronx.

CSci 127 (Hunter) Lecture 6 Summer 2020 24 / 35

Page 91: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Solutions

Predict what the following will do:

print("Queens:", pop["Queens"].min())

Minimum value in the column with label “Queens”.

print("S I:", pop["Staten Island"].mean())

Average of values in the column “Staten Island”.

print("S I :", pop["Staten Island"].std())

Standard deviation of values in the column “StatenIsland”.

pop.plot.bar(x="Year")

Bar chart with x-axis ”Year”.

pop.plot.scatter(x="Brooklyn", y= "Total")

Scatter plot of Brooklyn versus Total values.

pop["Fraction"] = pop["Bronx"]/pop["Total"]

New column with the fraction of population thatlives in the Bronx.

CSci 127 (Hunter) Lecture 6 Summer 2020 24 / 35

Page 92: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Solutions

Predict what the following will do:

print("Queens:", pop["Queens"].min())

Minimum value in the column with label “Queens”.

print("S I:", pop["Staten Island"].mean())

Average of values in the column “Staten Island”.

print("S I :", pop["Staten Island"].std())

Standard deviation of values in the column “StatenIsland”.

pop.plot.bar(x="Year")

Bar chart with x-axis ”Year”.

pop.plot.scatter(x="Brooklyn", y= "Total")

Scatter plot of Brooklyn versus Total values.

pop["Fraction"] = pop["Bronx"]/pop["Total"]

New column with the fraction of population thatlives in the Bronx.

CSci 127 (Hunter) Lecture 6 Summer 2020 24 / 35

Page 93: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Solutions

Predict what the following will do:

print("Queens:", pop["Queens"].min())

Minimum value in the column with label “Queens”.

print("S I:", pop["Staten Island"].mean())

Average of values in the column “Staten Island”.

print("S I :", pop["Staten Island"].std())

Standard deviation of values in the column “StatenIsland”.

pop.plot.bar(x="Year")

Bar chart with x-axis ”Year”.

pop.plot.scatter(x="Brooklyn", y= "Total")

Scatter plot of Brooklyn versus Total values.

pop["Fraction"] = pop["Bronx"]/pop["Total"]

New column with the fraction of population thatlives in the Bronx.

CSci 127 (Hunter) Lecture 6 Summer 2020 24 / 35

Page 94: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Solutions

Predict what the following will do:

print("Queens:", pop["Queens"].min())

Minimum value in the column with label “Queens”.

print("S I:", pop["Staten Island"].mean())

Average of values in the column “Staten Island”.

print("S I :", pop["Staten Island"].std())

Standard deviation of values in the column “StatenIsland”.

pop.plot.bar(x="Year")

Bar chart with x-axis ”Year”.

pop.plot.scatter(x="Brooklyn", y= "Total")

Scatter plot of Brooklyn versus Total values.

pop["Fraction"] = pop["Bronx"]/pop["Total"]

New column with the fraction of population thatlives in the Bronx.

CSci 127 (Hunter) Lecture 6 Summer 2020 24 / 35

Page 95: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Solutions

Predict what the following will do:

print("Queens:", pop["Queens"].min())

Minimum value in the column with label “Queens”.

print("S I:", pop["Staten Island"].mean())

Average of values in the column “Staten Island”.

print("S I :", pop["Staten Island"].std())

Standard deviation of values in the column “StatenIsland”.

pop.plot.bar(x="Year")

Bar chart with x-axis ”Year”.

pop.plot.scatter(x="Brooklyn", y= "Total")

Scatter plot of Brooklyn versus Total values.

pop["Fraction"] = pop["Bronx"]/pop["Total"]

New column with the fraction of population thatlives in the Bronx.

CSci 127 (Hunter) Lecture 6 Summer 2020 24 / 35

Page 96: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Solutions

Predict what the following will do:

print("Queens:", pop["Queens"].min())

Minimum value in the column with label “Queens”.

print("S I:", pop["Staten Island"].mean())

Average of values in the column “Staten Island”.

print("S I :", pop["Staten Island"].std())

Standard deviation of values in the column “StatenIsland”.

pop.plot.bar(x="Year")

Bar chart with x-axis ”Year”.

pop.plot.scatter(x="Brooklyn", y= "Total")

Scatter plot of Brooklyn versus Total values.

pop["Fraction"] = pop["Bronx"]/pop["Total"]

New column with the fraction of population thatlives in the Bronx.

CSci 127 (Hunter) Lecture 6 Summer 2020 24 / 35

Page 97: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem

cunyF2016.csv

Write a complete Python program thatreads in the file, cunyF2016.csv, andproduces a scatter plot of full-time ver-sus part-time enrollment.

Solution:

1 Include pandas & pyplot libraries.

2 Read in the CSV file.

3 Set up a scatter plot.

4 Display plot.

CSci 127 (Hunter) Lecture 6 Summer 2020 25 / 35

Page 98: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem

cunyF2016.csv

Write a complete Python program thatreads in the file, cunyF2016.csv, andproduces a scatter plot of full-time ver-sus part-time enrollment.

Solution:

1 Include pandas & pyplot libraries.

2 Read in the CSV file.

3 Set up a scatter plot.

4 Display plot.

CSci 127 (Hunter) Lecture 6 Summer 2020 25 / 35

Page 99: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem

cunyF2016.csv

Write a complete Python program thatreads in the file, cunyF2016.csv, andproduces a scatter plot of full-time ver-sus part-time enrollment.

Solution:

1 Include pandas & pyplot libraries.

2 Read in the CSV file.

3 Set up a scatter plot.

4 Display plot.

CSci 127 (Hunter) Lecture 6 Summer 2020 25 / 35

Page 100: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem

cunyF2016.csv

Write a complete Python program thatreads in the file, cunyF2016.csv, andproduces a scatter plot of full-time ver-sus part-time enrollment.

Solution:

1 Include pandas & pyplot libraries.

2 Read in the CSV file.

3 Set up a scatter plot.

4 Display plot.

CSci 127 (Hunter) Lecture 6 Summer 2020 25 / 35

Page 101: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem

cunyF2016.csv

Write a complete Python program thatreads in the file, cunyF2016.csv, andproduces a scatter plot of full-time ver-sus part-time enrollment.

Solution:

1 Include pandas & pyplot libraries.

2 Read in the CSV file.

3 Set up a scatter plot.

4 Display plot.

CSci 127 (Hunter) Lecture 6 Summer 2020 25 / 35

Page 102: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem

cunyF2016.csv

Write a complete Python program thatreads in the file, cunyF2016.csv, andproduces a scatter plot of full-time ver-sus part-time enrollment.

Solution:

1 Include pandas & pyplot libraries.

2 Read in the CSV file.

3 Set up a scatter plot.

4 Display plot.

CSci 127 (Hunter) Lecture 6 Summer 2020 25 / 35

Page 103: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem

cunyF2016.csv

Write a complete Python program thatreads in the file, cunyF2016.csv, andproduces a scatter plot of full-time ver-sus part-time enrollment.

Solution:

1 Include pandas & pyplot libraries.import matplotlib.pyplot as plt

import pandas as pd

2 Read in the CSV file.pop=pd.read csv(’cunyF2016.csv’,skiprows=1)

3 Set up a scatter plot.pop.plot.scatter(x="Full-time",y="Part-time")

4 Display plot.plt.show()

CSci 127 (Hunter) Lecture 6 Summer 2020 26 / 35

Page 104: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem

cunyF2016.csv

Write a complete Python program thatreads in the file, cunyF2016.csv, andproduces a scatter plot of full-time ver-sus part-time enrollment.

Solution:

1 Include pandas & pyplot libraries.

import matplotlib.pyplot as plt

import pandas as pd

2 Read in the CSV file.pop=pd.read csv(’cunyF2016.csv’,skiprows=1)

3 Set up a scatter plot.pop.plot.scatter(x="Full-time",y="Part-time")

4 Display plot.plt.show()

CSci 127 (Hunter) Lecture 6 Summer 2020 26 / 35

Page 105: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem

cunyF2016.csv

Write a complete Python program thatreads in the file, cunyF2016.csv, andproduces a scatter plot of full-time ver-sus part-time enrollment.

Solution:

1 Include pandas & pyplot libraries.import matplotlib.pyplot as plt

import pandas as pd

2 Read in the CSV file.pop=pd.read csv(’cunyF2016.csv’,skiprows=1)

3 Set up a scatter plot.pop.plot.scatter(x="Full-time",y="Part-time")

4 Display plot.plt.show()

CSci 127 (Hunter) Lecture 6 Summer 2020 26 / 35

Page 106: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem

cunyF2016.csv

Write a complete Python program thatreads in the file, cunyF2016.csv, andproduces a scatter plot of full-time ver-sus part-time enrollment.

Solution:

1 Include pandas & pyplot libraries.import matplotlib.pyplot as plt

import pandas as pd

2 Read in the CSV file.

pop=pd.read csv(’cunyF2016.csv’,skiprows=1)

3 Set up a scatter plot.pop.plot.scatter(x="Full-time",y="Part-time")

4 Display plot.plt.show()

CSci 127 (Hunter) Lecture 6 Summer 2020 26 / 35

Page 107: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem

cunyF2016.csv

Write a complete Python program thatreads in the file, cunyF2016.csv, andproduces a scatter plot of full-time ver-sus part-time enrollment.

Solution:

1 Include pandas & pyplot libraries.import matplotlib.pyplot as plt

import pandas as pd

2 Read in the CSV file.pop=pd.read csv(’cunyF2016.csv’,skiprows=1)

3 Set up a scatter plot.

pop.plot.scatter(x="Full-time",y="Part-time")

4 Display plot.plt.show()

CSci 127 (Hunter) Lecture 6 Summer 2020 26 / 35

Page 108: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem

cunyF2016.csv

Write a complete Python program thatreads in the file, cunyF2016.csv, andproduces a scatter plot of full-time ver-sus part-time enrollment.

Solution:

1 Include pandas & pyplot libraries.import matplotlib.pyplot as plt

import pandas as pd

2 Read in the CSV file.pop=pd.read csv(’cunyF2016.csv’,skiprows=1)

3 Set up a scatter plot.pop.plot.scatter(x="Full-time",y="Part-time")

4 Display plot.

plt.show()

CSci 127 (Hunter) Lecture 6 Summer 2020 26 / 35

Page 109: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Challenge Problem

cunyF2016.csv

Write a complete Python program thatreads in the file, cunyF2016.csv, andproduces a scatter plot of full-time ver-sus part-time enrollment.

Solution:

1 Include pandas & pyplot libraries.import matplotlib.pyplot as plt

import pandas as pd

2 Read in the CSV file.pop=pd.read csv(’cunyF2016.csv’,skiprows=1)

3 Set up a scatter plot.pop.plot.scatter(x="Full-time",y="Part-time")

4 Display plot.plt.show()

CSci 127 (Hunter) Lecture 6 Summer 2020 26 / 35

Page 110: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

groupby()

AustraliaRain.csv

Sometimes you have recurring valuesin a column and you want to examinethe data for a particular value.

For example, to find the average rain-fall at each location:

1 Import libraries.import pandas as pd

2 Read in the CSV file.rain =

pd.read csv(’AustraliaRain.csv’,skiprows=1)

3 Group the data by location averages.groupAvg =

rain.groupby(’Location’).mean()

4 Print the average rainfall at eachlocation.print(groupAvg[’Rainfall’])

CSci 127 (Hunter) Lecture 6 Summer 2020 27 / 35

Page 111: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

groupby()

AustraliaRain.csv

Sometimes you have recurring valuesin a column and you want to examinethe data for a particular value.

For example, to find the average rain-fall at each location:

1 Import libraries.import pandas as pd

2 Read in the CSV file.rain =

pd.read csv(’AustraliaRain.csv’,skiprows=1)

3 Group the data by location averages.groupAvg =

rain.groupby(’Location’).mean()

4 Print the average rainfall at eachlocation.print(groupAvg[’Rainfall’])

CSci 127 (Hunter) Lecture 6 Summer 2020 27 / 35

Page 112: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

groupby()

AustraliaRain.csv

Sometimes you have recurring valuesin a column and you want to examinethe data for a particular value.

For example, to find the average rain-fall at each location:

1 Import libraries.import pandas as pd

2 Read in the CSV file.rain =

pd.read csv(’AustraliaRain.csv’,skiprows=1)

3 Group the data by location averages.groupAvg =

rain.groupby(’Location’).mean()

4 Print the average rainfall at eachlocation.print(groupAvg[’Rainfall’])

CSci 127 (Hunter) Lecture 6 Summer 2020 27 / 35

Page 113: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

groupby()

AustraliaRain.csv

Sometimes you have recurring valuesin a column and you want to examinethe data for a particular value.

For example, to find the average rain-fall at each location:

1 Import libraries.import pandas as pd

2 Read in the CSV file.rain =

pd.read csv(’AustraliaRain.csv’,skiprows=1)

3 Group the data by location averages.groupAvg =

rain.groupby(’Location’).mean()

4 Print the average rainfall at eachlocation.print(groupAvg[’Rainfall’])

CSci 127 (Hunter) Lecture 6 Summer 2020 27 / 35

Page 114: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

groupby()

AustraliaRain.csv

Sometimes you have recurring valuesin a column and you want to examinethe data for a particular value.

For example, to find the average rain-fall at each location:

1 Import libraries.import pandas as pd

2 Read in the CSV file.rain =

pd.read csv(’AustraliaRain.csv’,skiprows=1)

3 Group the data by location averages.groupAvg =

rain.groupby(’Location’).mean()

4 Print the average rainfall at eachlocation.print(groupAvg[’Rainfall’])

CSci 127 (Hunter) Lecture 6 Summer 2020 27 / 35

Page 115: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

groupby()

AustraliaRain.csv

Sometimes you have recurring valuesin a column and you want to examinethe data for a particular value.

For example, to find the average rain-fall at each location:

1 Import libraries.import pandas as pd

2 Read in the CSV file.rain =

pd.read csv(’AustraliaRain.csv’,skiprows=1)

3 Group the data by location averages.groupAvg =

rain.groupby(’Location’).mean()

4 Print the average rainfall at eachlocation.print(groupAvg[’Rainfall’])

CSci 127 (Hunter) Lecture 6 Summer 2020 27 / 35

Page 116: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

groupby()

AustraliaRain.csv

Sometimes you have recurring valuesin a column and you want to examinethe data for a particular value.

For example, to find the average rain-fall at each location:

1 Import libraries.import pandas as pd

2 Read in the CSV file.rain =

pd.read csv(’AustraliaRain.csv’,skiprows=1)

3 Group the data by location averages.groupAvg =

rain.groupby(’Location’).mean()

4 Print the average rainfall at eachlocation.print(groupAvg[’Rainfall’])

CSci 127 (Hunter) Lecture 6 Summer 2020 28 / 35

Page 117: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

groupby()

AustraliaRain.csv

Sometimes you have recurring values in a col-umn and you want to examine the data for aparticular value.

For example, to find the average rainfall atone location, e.g. Moree:

1 Import libraries.import pandas as pd

2 Read in the CSV file.rain =

pd.read csv(’AustraliaRain.csv’,skiprows=1)

3 Group the data by location get averages forgroup Moree.MoreeAvg =

rain.groupby([’Location’]).get group(’Moree’).mean()

4 Print the average rainfall.print(MoreeAvg[’Rainfall’])

CSci 127 (Hunter) Lecture 6 Summer 2020 29 / 35

Page 118: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

groupby()

AustraliaRain.csv

Sometimes you have recurring values in a col-umn and you want to examine the data for aparticular value.

For example, to find the average rainfall atone location, e.g. Moree:

1 Import libraries.import pandas as pd

2 Read in the CSV file.rain =

pd.read csv(’AustraliaRain.csv’,skiprows=1)

3 Group the data by location get averages forgroup Moree.MoreeAvg =

rain.groupby([’Location’]).get group(’Moree’).mean()

4 Print the average rainfall.print(MoreeAvg[’Rainfall’])

CSci 127 (Hunter) Lecture 6 Summer 2020 29 / 35

Page 119: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

groupby()

AustraliaRain.csv

Sometimes you have recurring values in a col-umn and you want to examine the data for aparticular value.

For example, to find the average rainfall atone location, e.g. Moree:

1 Import libraries.import pandas as pd

2 Read in the CSV file.rain =

pd.read csv(’AustraliaRain.csv’,skiprows=1)

3 Group the data by location get averages forgroup Moree.MoreeAvg =

rain.groupby([’Location’]).get group(’Moree’).mean()

4 Print the average rainfall.print(MoreeAvg[’Rainfall’])

CSci 127 (Hunter) Lecture 6 Summer 2020 29 / 35

Page 120: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Today’s Topics

Recap: Logical Expressions & Circuits

Design: Cropping Images

Accessing Formatted Data

Design Challenge: Astrophysics andastropy

CSci 127 (Hunter) Lecture 6 Summer 2020 30 / 35

Page 121: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Design Challenge

On a piece of paper, design an algorithm that:

Prints the luminosity of the brightest star.

Prints the temperature in Kelvin (K) of the coldest star.

Prints the temperature in Fahrenheit of the coldest star. New:astropy.units will seamlessly convert!!!

Prints the average radius of a Hypergiant

CSci 127 (Hunter) Lecture 6 Summer 2020 31 / 35

Page 122: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Design Challenge - Solution

Libraries: pandas and astropy

Process:I Print max of ’Luminosity’ columnI Prints min of ’Temperature’ column and store it in temp variableI Use astropy to convert temp variable to Fahrenheit and printI groupby ’Star Type’ and take averages, then print max of ’Radius’

columnI OR groupby ’Star Type’ and get group ’Hypergiant’ to print

average ’Radius’

CSci 127 (Hunter) Lecture 6 Summer 2020 32 / 35

Page 123: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Design Challenge - Solution

Libraries: pandas and astropy

Process:I Print max of ’Luminosity’ column

I Prints min of ’Temperature’ column and store it in temp variableI Use astropy to convert temp variable to Fahrenheit and printI groupby ’Star Type’ and take averages, then print max of ’Radius’

columnI OR groupby ’Star Type’ and get group ’Hypergiant’ to print

average ’Radius’

CSci 127 (Hunter) Lecture 6 Summer 2020 32 / 35

Page 124: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Design Challenge - Solution

Libraries: pandas and astropy

Process:I Print max of ’Luminosity’ columnI Prints min of ’Temperature’ column and store it in temp variable

I Use astropy to convert temp variable to Fahrenheit and printI groupby ’Star Type’ and take averages, then print max of ’Radius’

columnI OR groupby ’Star Type’ and get group ’Hypergiant’ to print

average ’Radius’

CSci 127 (Hunter) Lecture 6 Summer 2020 32 / 35

Page 125: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Design Challenge - Solution

Libraries: pandas and astropy

Process:I Print max of ’Luminosity’ columnI Prints min of ’Temperature’ column and store it in temp variableI Use astropy to convert temp variable to Fahrenheit and print

I groupby ’Star Type’ and take averages, then print max of ’Radius’column

I OR groupby ’Star Type’ and get group ’Hypergiant’ to printaverage ’Radius’

CSci 127 (Hunter) Lecture 6 Summer 2020 32 / 35

Page 126: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Design Challenge - Solution

Libraries: pandas and astropy

Process:I Print max of ’Luminosity’ columnI Prints min of ’Temperature’ column and store it in temp variableI Use astropy to convert temp variable to Fahrenheit and printI groupby ’Star Type’ and take averages, then print max of ’Radius’

column

I OR groupby ’Star Type’ and get group ’Hypergiant’ to printaverage ’Radius’

CSci 127 (Hunter) Lecture 6 Summer 2020 32 / 35

Page 127: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Design Challenge - Solution

Libraries: pandas and astropy

Process:I Print max of ’Luminosity’ columnI Prints min of ’Temperature’ column and store it in temp variableI Use astropy to convert temp variable to Fahrenheit and printI groupby ’Star Type’ and take averages, then print max of ’Radius’

columnI OR groupby ’Star Type’ and get group ’Hypergiant’ to print

average ’Radius’

CSci 127 (Hunter) Lecture 6 Summer 2020 32 / 35

Page 128: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Design Challenge - Code

Libraries: pandas and astropyimport pandas as pd

import astropy.units as u

stars = pd.read csv(’Stars.csv’)

Process:I Print max of ’Luminosity’ column

print(stars[’Luminosity(L/Lo)’].max())I Prints min of ’Temperature’ column and store it in temp variable

minTempK = stars[’Temperature (K)’].min()

print(minTempK)I Use astropy to convert temp variable to Fahrenheit and print

KUnit = minTempK * u.K

print(KUnit.to(u.imperial.deg F, equivalencies = \u.temperature()))

I groupby ’Star Type’ and take averages, then print max of ’Radius’columnprint(stars.groupby([’Star type’])\.mean()[’Radius(R/Ro)’].max())

CSci 127 (Hunter) Lecture 6 Summer 2020 33 / 35

Page 129: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Design Challenge - Code

Libraries: pandas and astropyimport pandas as pd

import astropy.units as u

stars = pd.read csv(’Stars.csv’)Process:

I Print max of ’Luminosity’ columnprint(stars[’Luminosity(L/Lo)’].max())

I Prints min of ’Temperature’ column and store it in temp variableminTempK = stars[’Temperature (K)’].min()

print(minTempK)I Use astropy to convert temp variable to Fahrenheit and print

KUnit = minTempK * u.K

print(KUnit.to(u.imperial.deg F, equivalencies = \u.temperature()))

I groupby ’Star Type’ and take averages, then print max of ’Radius’columnprint(stars.groupby([’Star type’])\.mean()[’Radius(R/Ro)’].max())

CSci 127 (Hunter) Lecture 6 Summer 2020 33 / 35

Page 130: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Design Challenge - Code

Libraries: pandas and astropyimport pandas as pd

import astropy.units as u

stars = pd.read csv(’Stars.csv’)Process:

I Print max of ’Luminosity’ columnprint(stars[’Luminosity(L/Lo)’].max())

I Prints min of ’Temperature’ column and store it in temp variableminTempK = stars[’Temperature (K)’].min()

print(minTempK)

I Use astropy to convert temp variable to Fahrenheit and printKUnit = minTempK * u.K

print(KUnit.to(u.imperial.deg F, equivalencies = \u.temperature()))

I groupby ’Star Type’ and take averages, then print max of ’Radius’columnprint(stars.groupby([’Star type’])\.mean()[’Radius(R/Ro)’].max())

CSci 127 (Hunter) Lecture 6 Summer 2020 33 / 35

Page 131: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Design Challenge - Code

Libraries: pandas and astropyimport pandas as pd

import astropy.units as u

stars = pd.read csv(’Stars.csv’)Process:

I Print max of ’Luminosity’ columnprint(stars[’Luminosity(L/Lo)’].max())

I Prints min of ’Temperature’ column and store it in temp variableminTempK = stars[’Temperature (K)’].min()

print(minTempK)I Use astropy to convert temp variable to Fahrenheit and print

KUnit = minTempK * u.K

print(KUnit.to(u.imperial.deg F, equivalencies = \u.temperature()))

I groupby ’Star Type’ and take averages, then print max of ’Radius’columnprint(stars.groupby([’Star type’])\.mean()[’Radius(R/Ro)’].max())

CSci 127 (Hunter) Lecture 6 Summer 2020 33 / 35

Page 132: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Design Challenge - Code

Libraries: pandas and astropyimport pandas as pd

import astropy.units as u

stars = pd.read csv(’Stars.csv’)Process:

I Print max of ’Luminosity’ columnprint(stars[’Luminosity(L/Lo)’].max())

I Prints min of ’Temperature’ column and store it in temp variableminTempK = stars[’Temperature (K)’].min()

print(minTempK)I Use astropy to convert temp variable to Fahrenheit and print

KUnit = minTempK * u.K

print(KUnit.to(u.imperial.deg F, equivalencies = \u.temperature()))

I groupby ’Star Type’ and take averages, then print max of ’Radius’column

print(stars.groupby([’Star type’])\.mean()[’Radius(R/Ro)’].max())

CSci 127 (Hunter) Lecture 6 Summer 2020 33 / 35

Page 133: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Design Challenge - Code

Libraries: pandas and astropyimport pandas as pd

import astropy.units as u

stars = pd.read csv(’Stars.csv’)Process:

I Print max of ’Luminosity’ columnprint(stars[’Luminosity(L/Lo)’].max())

I Prints min of ’Temperature’ column and store it in temp variableminTempK = stars[’Temperature (K)’].min()

print(minTempK)I Use astropy to convert temp variable to Fahrenheit and print

KUnit = minTempK * u.K

print(KUnit.to(u.imperial.deg F, equivalencies = \u.temperature()))

I groupby ’Star Type’ and take averages, then print max of ’Radius’columnprint(stars.groupby([’Star type’])\.mean()[’Radius(R/Ro)’].max())

CSci 127 (Hunter) Lecture 6 Summer 2020 33 / 35

Page 134: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Design Challenge - Code

Libraries: pandas and astropyimport pandas as pd

import astropy.units as u

stars = pd.read csv(’Stars.csv’)Process:

I Print max of ’Luminosity’ columnprint(stars[’Luminosity(L/Lo)’].max())

I Prints min of ’Temperature’ column and store it in temp variableminTempK = stars[’Temperature (K)’].min()

print(minTempK)I Use astropy to convert temp variable to Fahrenheit and print

KUnit = minTempK * u.K

print(KUnit.to(u.imperial.deg F, equivalencies = \u.temperature()))

I OR groupby ’Star Type’ and get group ’Hypergiant’ to printaverage ’Radius’print(stars.groupby([’Star type’])\.get group(’Hypergiant’).mean()[’Radius(R/Ro)’])

CSci 127 (Hunter) Lecture 6 Summer 2020 34 / 35

Page 135: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Design Challenge - Code

Libraries: pandas and astropyimport pandas as pd

import astropy.units as u

stars = pd.read csv(’Stars.csv’)Process:

I Print max of ’Luminosity’ columnprint(stars[’Luminosity(L/Lo)’].max())

I Prints min of ’Temperature’ column and store it in temp variableminTempK = stars[’Temperature (K)’].min()

print(minTempK)I Use astropy to convert temp variable to Fahrenheit and print

KUnit = minTempK * u.K

print(KUnit.to(u.imperial.deg F, equivalencies = \u.temperature()))

I OR groupby ’Star Type’ and get group ’Hypergiant’ to printaverage ’Radius’print(stars.groupby([’Star type’])\.get group(’Hypergiant’).mean()[’Radius(R/Ro)’])

CSci 127 (Hunter) Lecture 6 Summer 2020 34 / 35

Page 136: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Recap

Recap: Logical Expressions & Circuits

Accessing Formatted Data:

I Pandas library has elegant solutions foraccessing & analyzing structured data.

I Can manipulate individual columns orrows (’Series’).

I Has useful functions for the entiresheet (’DataFrame’) such as plotting.

Log in to Gradescope for Quiz 6.

CSci 127 (Hunter) Lecture 6 Summer 2020 35 / 35

Page 137: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Recap

Recap: Logical Expressions & Circuits

Accessing Formatted Data:

I Pandas library has elegant solutions foraccessing & analyzing structured data.

I Can manipulate individual columns orrows (’Series’).

I Has useful functions for the entiresheet (’DataFrame’) such as plotting.

Log in to Gradescope for Quiz 6.

CSci 127 (Hunter) Lecture 6 Summer 2020 35 / 35

Page 138: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Recap

Recap: Logical Expressions & Circuits

Accessing Formatted Data:

I Pandas library has elegant solutions foraccessing & analyzing structured data.

I Can manipulate individual columns orrows (’Series’).

I Has useful functions for the entiresheet (’DataFrame’) such as plotting.

Log in to Gradescope for Quiz 6.

CSci 127 (Hunter) Lecture 6 Summer 2020 35 / 35

Page 139: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Recap

Recap: Logical Expressions & Circuits

Accessing Formatted Data:

I Pandas library has elegant solutions foraccessing & analyzing structured data.

I Can manipulate individual columns orrows (’Series’).

I Has useful functions for the entiresheet (’DataFrame’) such as plotting.

Log in to Gradescope for Quiz 6.

CSci 127 (Hunter) Lecture 6 Summer 2020 35 / 35

Page 140: CSci 127: Introduction to Computer Science · Yes, we will, since 1) it’s fundamental, and 2) the same ideas are used for accessing formatted data (today’s topic). I still don’t

Recap

Recap: Logical Expressions & Circuits

Accessing Formatted Data:

I Pandas library has elegant solutions foraccessing & analyzing structured data.

I Can manipulate individual columns orrows (’Series’).

I Has useful functions for the entiresheet (’DataFrame’) such as plotting.

Log in to Gradescope for Quiz 6.

CSci 127 (Hunter) Lecture 6 Summer 2020 35 / 35