Profile picture

1.7 - Pandas Series

Last updated: February 21st, 20192019-02-21Project preview

rmotr


Pandas Series - Exercises

In [1]:
import numpy as np
import pandas as pd

# This will help the formatting in the notebook, it's not important:
pd.options.display.float_format = '{:20,.2f}'.format

purple-divider

Exercise 1

Using Pandas, create a Series with numbers from 10 to 15, and float type.

In [ ]:
# your code goes here
In [ ]:
series = pd.Series(data=np.arange(10, 16),
                   dtype=np.float64)
series

green-divider

Exercise 2

Add indexes from A to F to the previous series.

In [ ]:
# your code goes here
In [ ]:
series.index = ['A', 'B', 'C', 'D', 'E', 'F']

series

green-divider

Exercise 3

Add a new element to the series: G: 20.0.

In [ ]:
# your code goes here
In [ ]:
series['G'] = 20.0

series

green-divider

Exercise 4

Remove the first element from the series.

In [ ]:
# your code goes here
In [ ]:
series.drop('A', inplace=True) # also, del series['A']

series

green-divider

Exercise 5

Get the value at D index.

In [ ]:
# your code goes here
In [ ]:
series['D']

green-divider

Exercise 6

Get the values from B to E indexes.

In [ ]:
# your code goes here
In [ ]:
series['B':'E']

green-divider

Exercise 7

Get all the values higher than 12.

In [ ]:
# your code goes here
In [ ]:
series[series > 12.0]

green-divider

Exercise 8

Get the mean of all the series elements.

In [ ]:
# your code goes here
In [ ]:
series.mean()

green-divider

Exercise 9

Add 10 to the values higher than 14.

In [ ]:
# your code goes here
In [ ]:
series[series > 14.0] += 10

series

green-divider

Exercise 10

Double series elements lower than 14.

In [ ]:
# your code goes here
In [ ]:
series[series < 14.0] = 2 * series[series < 14.0]

series

purple-divider

For the following exercises, we'll read the csv file located in data/salaries.csv, that contains salaries of NBA Players (original can be found here).

We haven't dug into reading CSV files yet (there's a special lesson about that later), so we've already included the line that reads the salaries into a series.

In [3]:
salaries = pd.read_csv('data/salaries.csv', squeeze=True, index_col=0)

salaries.head().to_frame()
Out[3]:
Salary
Player
Stephen Curry 34,682,550.00
LeBron James 33,285,709.00
Paul Millsap 31,269,231.00
Gordon Hayward 29,727,900.00
Blake Griffin 29,512,900.00

green-divider

Exercise 11

Using Pandas, create a Series with numbers from 10 to 15, and float type.

How many players does the series salaries have?

In [ ]:
# your code goes here
In [7]:
salaries.size
Out[7]:
573

green-divider

Exercise 12

What are the highest and lowest salaries on the league?

In [4]:
# your code goes here
In [5]:
# Highest:
salaries.max()
Out[5]:
34682550.0
In [6]:
# Lowest:
salaries.min()
Out[6]:
17224.0

green-divider

Exercise 13

Show us LeBron James's salary (make sure you don't make a typo with his name, you'll get a KeyError otherwise):

In [9]:
# your code goes here
In [10]:
salaries.loc['LeBron James']
Out[10]:
33285709.0

green-divider

Exercise 14

Show us the first 10 players in the series:

In [ ]:
# your code goes here
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 

purple-divider

Notebooks AI
Notebooks AI Profile20060