Pandas Series Flashcards

1
Q

data = pd.Series((0.25, 0.5, 0.75, 1.0), )

A

A Pandas Series is a one-dimensional array of indexed data. It can be created from a list or array as follows:

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

data.values

A

The values are simply a familiar NumPy array

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

data.index

A

The index is an array-like object of type pd.Index

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

data[1:3]

A

Like with a NumPy array, data can be accessed by the associated index via the familiar Python square-bracket notation

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

data = pd.Series([0.25, 0.5, 0.75, 1.0],
index=[‘a’, ‘b’, ‘c’, ‘d’])
data

A

This explicit index definition gives the Series object additional capabilities. For example, the index need not be an integer, but can consist of values of any desired type. For example, if we wish, we can use strings as an index:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
population_dict = {'California': 38332521,
                   'Texas': 26448193,
                   'New York': 19651127,
                   'Florida': 19552860,
                   'Illinois': 12882135}
population = pd.Series(population_dict)
population
A

The Series-as-dictionary analogy can be made even more clear by constructing a Series object directly from a Python dictionary

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

population[‘California’:’Illinois’]

A

Unlike a dictionary, though, the Series also supports array-style operations such as slicing:

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

pd.Series(5.0, index=[100, 200, 300])

A

data can be a scalar, which is repeated to fill the specified index:

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

pd.Series({2:’a’, 1:’b’, 3:’c’}, index=[3])

A

Notice that in this case, the Series is populated only with the explicitly identified keys.

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

states = pd.DataFrame((population, area), columns=[‘population’, ‘area’])

A

we can use a dictionary to construct a single two-dimensional object containing this information:
(Matching Indexes)

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

states.index

A

Like the Series object, the DataFrame has an index attribute that gives access to the index labels:

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

states.columns

A

Additionally, the DataFrame has a columns attribute, which is an Index object holding the column labels:

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

A DataFrame is a collection of Series objects, and a single-column DataFrame can be constructed from a single Series:

Any list of dictionaries can be made into a DataFrame

Even if some keys in the dictionary are missing, Pandas will fill them in with NaN (i.e., “not a number”) values:

A

Dataframe

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

pd.DataFrame({‘population’: population,

‘area’: area})

A

DataFrame can be constructed from a dictionary of Series objects as well:

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

pd.DataFrame(np.random.rand(3, 2),
columns=[‘foo’, ‘bar’],
)

A

Given a two-dimensional array of data, we can create a DataFrame with any specified column and index names. If omitted, an integer index will be used for each:

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

Index
ind[1] = 0
ERROR

A

One difference between Index objects and NumPy arrays is that indices are immutable–that is, they cannot be modified via the normal means:

17
Q
indA = pd.Index([1, 3, 5, 7, 9])
indB = pd.Index([2, 3, 5, 7, 11])

indA & indB # intersection
indA | indB # union
indA ^ indB # symmetric difference

A

Pandas objects are designed to facilitate operations such as joins across datasets, which depend on many aspects of set arithmetic. The Index object follows many of the conventions used by Python’s built-in set data structure, so that unions, intersections, differences, and other combinations can be computed in a familiar way: