Shared Flashcard Set

Details

Pandas series
Pandas series for use in data science
16
Computer Science
Undergraduate 3
02/17/2021

Additional Computer Science Flashcards

 


 

Cards

Term

Write an expression to create a Pandas series from 1D NumPy array x.

Definition

pd.Series(x) 

Term

What is the result of the following code  (say 'error' if error)?

 

x = pd.Series([4, 2, 7, 8])

x.values

Definition

Array [4, 2, 7, 8]

Term

What is the result of the following code  (say 'error' if error)?

 

x = pd.Series([1,2,3], index=['a','b','a'])

x['a']

Definition

The series with values [1,3] and index ['a', 'a'].

Term

What is the result of the following code  (say 'error' if error)?

 

x = pd.Series([0.25,0.5,0.75,1.0],

              index=['a','b','c','a'])

x[2]

Definition

0.75

Term

What is the result of the following code  (say 'error' if error)?

 

x = pd.Series([0.25,0.5,0.75,1.0],

              index=['a','b','c','a'])

x[:2]

Definition

A series with values [0.25, 0.5]

Term

What is the result of the following code  (say 'error' if error)?

 

x = pd.Series([0.25,0.5,0.75,1.0],

              index=['a','b','c','a'])

x[x < 0.6]

Definition

The series with values [0.25, 0.5] and index ['a','b']

Term

What is the result of the following code  (say 'error' if error)?

 

x = pd.Series([0.25,0.5,0.75,1.0],

              index=['a','b','c','a'])

x[(x > 0.3) and (x < 0.9)]

Definition

Error.  "The truth value of a series is ambiguous."

You can't use Python's 'and' to get the logical and of two masks.

Term

What is the result of the following code  (say 'error' if error)?

 

x = pd.Series([0.25,0.5,0.75,1.0],

              index=['a','b','c','a'])

x > 0.3

Definition

A Pandas series with values [False, True, True, True] and index ['a','b','c','a'].

Term

What is the result of the following code  (say 'error' if error)?

 

x = pd.Series([0.25,0.5,0.75,1.0],

              index=['a','b','c','a'])

x > 0.3 & x < 0.9

Definition

Error.  You need parentheses around the conditions.

If it were written (x > 0.3) & (x < 0.9) it would give  the Pandas series with values [False, True, True, False].

Term

Is the result of the following code a Pandas series or a NumPy array?

 

x = pd.Series([0.25,0.5,0.75,1.0],

              index=['a','b','c','a'])

x[[0,3]]

Definition

Pandas series.  The result is a series with values [0.25, 1.00]

Term

What is the result of the following code  (say 'error' if error)?

 

x = pd.Series([0.25,0.5,0.75,1.0],

              index=[3,6,0,1])

x.loc[1]

Definition

1.0

.loc uses the explicit index

Term

What is the result of the following code  (say 'error' if error)?

 

x = pd.Series([0.25,0.5,0.75,1.0],

              index=[3,6,0,1])

x.iloc[1]

Definition

0.5

.iloc uses the implicit index

Term

What is the result of the following code  (say 'error' if error)?

 

x = pd.Series([0.25,0.5,0.75,1.0],

              index=[3,6,0,1])

x.iloc(1)

Definition

Error.  With .iloc you use square brackets, not parentheses.

Term

What is the result of the following code  (say 'error' if error)?

 

x = pd.Series([0.25,0.5,0.75,1.0])

x * x

Definition

The series with values [0.0625, 0.25, 0.5625, 1.0]

 

(0.0625 is 0.25 * 0.25, 0.25 is 0.5 * 0.5, etc.)

 

Term

What is the result of the following code  (say 'error' if error)?

 

x = pd.Series([0.25,0.5,0.75,1.0])

y = pd.Series([1, 2, 3, 4])

x + y

Definition

The series with values [1.25, 2.5, 3.75, 5.0]

Term

What is the result of the following code  (say 'error' if error)?

 

x = pd.Series([0.25,0.5,0.75,1.0])

y = pd.Series([1,2,1,0], index=['a','b','c','d'])

x < y

Definition

Error, because the indexes of the series are different.

 

"ValueError: Can only compare identically-labeled Series objects"

Supporting users have an ad free experience!