• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 2 out of 12 pages
Summary

Summary Solutions on Functions and Visualizations in Python

Document preview thumbnail
Preview 2 out of 12 pages

Question 2. You should have found that the values in "Total Pay" column are strings (text). It doesn't make sense to take the average of the text values, so we need to convert them to numbers if we want to do this. Extract the first value in the "Total Pay" column. It's Mark Hurd's pay in 2015, in millions of dollars. Call it mark_hurd_pay_string . In [ ]: mark_hurd_pay_string UTION = mark_hurd_pay_string raw_compensation . column ( "Total Pay" ) . item In [ ]: _ = ok . grade ( 'q1_2' ) ( 0 ) #SOL Question 3. Convert mark_hurd_pay_string to a number of dollars. The string method strip will be useful for removing the dollar sign; it removes a specified character from the start or end of a string. For example, the value of "100%".strip("%") is the string "100". You'll also need the function float, which converts a string that looks like a number to an actual number. Last, remember that the answer should be in dollars, not millions of dollars. In [ ]: mark_hurd_pay = mark_hurd_pay 10 ** 6 * float ( mark_hurd_pay_string . strip ( In [ ]: _ = ok . grade ( 'q1_3' )

Content preview

lOMoAR cPSD| 70487227




lOMoAR cPSD| 70487227




Data-8 Solutions on Functions and Visualizations in
Python
Functions and Visualizations
First, set up the tests and imports by running the cell below.

In [ ]: import numpy as np
from datascience import *

# These lines set up graphing capabilities.
import matplotlib
%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
import warnings
warnings.simplefilter('ignore', FutureWarning)

from client.api.notebook import Notebook
ok = Notebook('lab04.ok')
_ = ok.auth(inline=True)




1. Functions and CEO Incomes
Let's start with a real data analysis task. We'll look at the 2015 compensation of CEOs at the 100 largest
companies in California. The data were compiled for a Los Angeles Times analysis here
(http://spreadsheets.latimes.com/california-ceo-compensation/), and ultimately came from filings
(https://www.sec.gov/answers/proxyhtf.htm) mandated by the SEC from all publicly-traded companies. Two
companies have two CEOs, so there are 102 CEOs in the dataset.

We've copied the data in raw form from the LA Times page into a file called raw_compensation.csv. (The
page notes that all dollar amounts are in millions of dollars.)


In [ ]: raw_compensation = Table.read_table('raw_compensation.csv')
raw_compensation



Question 1. We want to compute the average of the CEOs' pay. Try running the cell below.


In [ ]: np.average(raw_compensation.column("Total Pay"))

, lOMoAR cPSD| 70487227




You should see an error. Let's examine why this error occured by looking at the values in the "Total Pay"
column. Use the type function and set total_pay_type to the type of the first value in the "Total Pay"
column.

In [ ]: total_pay_type = type(raw_compensation.column("Total Pay").item(0)) #SOL
UTION
total_pay_type

In [ ]: _ = ok.grade('q1_1')



Question 2. You should have found that the values in "Total Pay" column are strings (text). It doesn't make
sense to take the average of the text values, so we need to convert them to numbers if we want to do this.
Extract the first value in the "Total Pay" column. It's Mark Hurd's pay in 2015, in millions of dollars. Call it
mark_hurd_pay_string.


In [ ]: mark_hurd_pay_string = raw_compensation.column("Total Pay").item(0) #SOL
UTION
mark_hurd_pay_string


In [ ]: _ = ok.grade('q1_2')



Question 3. Convert mark_hurd_pay_string to a number of dollars. The string method strip will be useful
for removing the dollar sign; it removes a specified character from the start or end of a string. For example, the
value of "100%".strip("%") is the string "100". You'll also need the function float, which converts a string
that looks like a number to an actual number. Last, remember that the answer should be in dollars, not millions
of dollars.

In [ ]: mark_hurd_pay = 10**6 * float(mark_hurd_pay_string.strip("$")) #SOLUTION
mark_hurd_pay


In [ ]: _ = ok.grade('q1_3')


To compute the average pay, we need to do this for every CEO. But that looks like it would involve copying this
code 102 times.

This is where functions come in. First, we'll define a new function, giving a name to the expression that converts
"total pay" strings to numeric values. Later in this lab we'll see the payoff: we can call that function on every pay
string in the dataset at once.

Question 4. Copy the expression you used to compute mark_hurd_pay as the return expression of the
function below, but replace the specific mark_hurd_pay_string with the generic pay_string name
specified in the first line of the def statement.

Document information

Uploaded on
September 18, 2026
Number of pages
12
Written in
2026/2027
Type
Summary
$10.99

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
0
Followers
0
Items
176
Last sold
-



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions