1. Series Flashcards

1
Q

what is a series

A

one dimensional labeled array

one dimensional means 1 key to get the value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

creating a series

A

lottery = [4,8,5,6,42,23]

pd.Series(lottery)

if it is a dictionary, the key will go into pd index
index doesn’t need to be unique

**Assign index
Fruits = [“Apple”,”Orange”,”Plum”]
weekdays = [“Monday”,”Tuesday”,”Wednesday”]
pd.Series(Fruits, index = weekdays)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

attribute vs method

A

attribute just tells you some info, no () at the end

methods do something on it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

3 basic series attribute

A

s. values => return array
s. index => return RangeIndex object
s. dtype

s. shape => Tuple
s. size (count null rows)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

basic calculation method

A

s. sum()
s. product()
s. mean()
s. count() exclude null values, while len(s) does not
s. describe()
s. max() vs s.idxmax()
s. min() vs s.idxmin()

s. head(n)
s. tail(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

read csv method

A

pd. read_csv(“pokemon.csv”) => df
pd. read_csv(“pokemon.csv”, usecols=[“Pokemon”])
pd. read_csv(“pokemon.csv”, usecols=[“Pokemon”], squeeze = True) => series

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

sort

A

s. sort_values()
s. sort_index()

**Inplace
google = google.sort_values()
or just
google.sort_values(inplace = True)

Note:
sort_values() will have the same index,
pokemon[0] doesn’t mean the first one in the series but the index with 0. because the “0” refer to a key first, then position
if the index is not a number then it’s the first one
however, slicing follows pokemon[0:50] position

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

check if something is in a series

A

by default it look into the key

“Bulbasaur” in pokemon.values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

extracting multiple member

A

pokemon[[459,62]]
pokemon.reindex(index = [459,62])
both return a new seires

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

.get() method on Series

A

s.get([list of valid keys], default=”abc”)
if any of the key does not exist, just return default and nothing else
can use reindex to bypass

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

count all the unique values

A

s.value_counts()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

apply method

A

s. apply(myfunc)

s. apply(lambda price: price ** 2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

map method

A

similar to vlookup, pass the value into key (from the original series) and return the values in second series

pokemon_names.map(pokemon_types)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

squeeze

A

nba = pd.read_csv(“nba.csv”, usecols= [“Team”,”Name”],index_col = “Name”, squeeze= True)

turn df into Series

nba = pd.read_csv(“nba.csv”, usecols= [“Team”],index_col = “Name”, squeeze= True)
This won’t work as not reading in the index column

How well did you know this?
1
Not at all
2
3
4
5
Perfectly