Pandas Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

How to create a DataFrame and a Series?

A

pd. DataFrame({“Bob”:[0,3,9], “Tim”:[1,2,3]}, index=[“Product A, Product B])
pd. Series([3,5,6,7], index=[“Product A, Product B])

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

How to let a column become the index?

A

object.set_index(“column name”)

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

Read in csv if csv has index itself:

A

…, index_col=0

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

Acess column:

A

object[“column”]

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

Acess with loc and iloc:

A

loc (needs column name) and iloc (both row, column): object.iloc[3,6] or object.loc[5, “houses”]

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

How large is DF:

A

object.shape

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

Assigning data

A

reviews[“critics]=”everyone”

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

Acess specific value:

A

object[“column”] [2]

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

Set up condition:

2 Conditions:

A

reviews. loc[reviews.country==”Italy”]

reviews. loc[(reviews.country==”Italy”) & reviews.points < 90)]

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

Multi Index: which p.e. occurs when you have filtered with 2 columns

A

object.reset_index()

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

How to sort by values and how to sort by index?

A

object.sort_values(by=”column name”, ascending=False). For 2 or more: list with column names.
Object.sort_index()= to sort by index values

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

How to ask for a datatype and how to transform one?

A
Object.column.dtype= to ask
Object.column.astype(“…”) = to transform
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to rename a column or an index?

A

object. rename(columns={“points”: “score”})

object. rename(index={0: “first”, 1: “second”})

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

Combine DataFrame and/or Series?

How if index in common?

A

pd.concat([object 1, object 2]) if same columns

left=canada_yt.set_index([“title”, “trending”])
right=usa_yt.set_index([“title”, “trending”])
left.join(right, lsuffix=”_can”, rsuffix=”_usa”)
->suffixe are only necessary because there are same column names in bith datasets

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