pandas 1_4 Flashcards

(52 cards)

1
Q

How do you write a multi-line string in Python?

A

””” wrap it in three quotes “””

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

(Python) How would you refer to the first element in a list named my_list?

A

my_list[0]

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

(Python) How would you refer to the last element in a list named my_list?

A

my_list[-1]

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

(Python) How would you refer to the first two elements in a list named my_list?

A

my_list[0:2]

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

(Python) What is a dict surrounded by?

A

curly brackets

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

(Python) How would you access 1 in my_dict = {‘a’: 1, ‘b’: 2}?

A

my_dict[‘a’]

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

(Python) How would you change ‘a’ to 0 in my_dict = {‘a’: 1, ‘b’: 2}?

A

my_dict[‘a’] = 0

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

(Python) How would you add a new item ‘c’ = 3 in my_dict = {‘a’: 1, ‘b’: 2}?

A

my_dict[‘c’] = 3

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

(Python) What does this code do?
for x in my_roster_list:
print(x)<br></br><br></br>

A

prints the values of my_roster_list<br></br>

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

(Python) What would type([x*2 for x in [1, 2, 3]) return?

A

list

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

(Python) What is [x*2 for x in [1, 2, 3]] called?

A

list comprehension

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

(Python) How would you turn mydict = {‘a’: 1, ‘b’: 2} into {‘A’: 2, ‘B’: 4} using a dict comprehension?

A

{key.upper(): value*2 for key, value in mydict.items()}

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

(Python) How would you turn mydict = {‘a’: 1, ‘b’: 2} into a list [1, 2] using list comprehensions?

A

[x for _, x in mydict.items()]

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

(Pandas) How would you print the first five rows of a DataFrame df?

A

df.head()

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

(Pandas) What is the function to load a csv into a DataFrame?

A

pd.read_csv()

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

(Pandas) What are the two main data structures Pandas gives you?

A

DataFrame, Series

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

(Pandas) What is the type of a single DataFrame column?

A

Series

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

(Pandas) What is the method to change the index on a DataFrame?

A

set_index

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

(Pandas) What is the default index on a DataFrame?

A

0, 1, 2, … for however many rows are in your data

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

(Pandas) What is the method to output a DataFrame to a csv?

A

to_csv

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

(Pandas) Why do you need the inplace=True argument to make permanent changes to DataFrames via methods?

A

because otherwise most methods only return a copy of the DataFrame and leave the original untouched

22
Q

(Pandas) How would you add a column named ‘pts_per_td’ that always equaled 6 to a DataFrame named df?

A

df[‘pts_per_td’] = 6

23
Q

(Pandas) What is the name of the method that lets you use functions like upper and replace on your string columns?

24
Q

(Pandas) How do you write ‘or’ when creating boolean columns in Pandas?

25
(Pandas) How do you write 'and' when creating boolean columns in Pandas?
&
26
(Pandas) How do you negate a boolean column in Pandas?
~
27
(Pandas) What is the method you use to call a function on every value in a column?
apply
28
(Pandas) What does apply take as an argument?
a function you want to call on every observation in a column in your DataFrame
29
(Pandas) When dropping a column in a DataFrame using the drop method, what argument do you have to pass so that it drops a column instead of a row?
axis=1
30
(Pandas) What keyword argument would you pass the rename function to rename a column 'old' to 'new'?
columns={'old': 'new'}
31
(Pandas) What are missing values in Pandas?
np.nan
32
(Pandas) What is the DataFrame/Series method that returns True if a value is missing, False if not?
isnull()
33
(Pandas) What is the DataFrame/Series method that returns True if a value is NOT missing, False if not?
notnull()
34
(Pandas) What is the method to replace np.nan with values of your choice?
filla()
35
(Pandas) What method lets you convert between column types?
astype()
36
(Pandas) What attribute lets you view the types of all your columns in a DataFrame?
dtypes
37
(Pandas) What does mean(axis=1) calculate the average over?
rows
38
(Pandas) What is the function to test whether any value of a boolean column is True?
any
39
(Pandas) What is the function to test whether all values of a boolean column are True?
all
40
(Pandas) How would you get the frequency of values for df['pos'] in Pandas?
df['pos'].value_counts()
41
(Pandas) Given some list of index values, how would you make a DataFrame of only those values?
pass them to loc
42
(Pandas) What is the second (optional) argument to loc?
column(s) you want to include
43
(Pandas) When you pass a (similarly index) boolean column to loc, what will it return?
only the rows where the boolean column is True
44
(Pandas) What does boolean indexing require re: the index of your DataFrame and the boolean column you're working with?
that they have the same index
45
(Pandas) How would you filter to only the instances where pos == 'RB' in a DataFrame df using loc?
df.loc[df['pos'] == 'RB']
46
(Pandas) What is the DataFrame method to drop duplicates?
drop_duplicates
47
(Pandas) What is the function to return a boolean column indicating whether a row is a duplicate?
duplicated
48
(Pandas) How would you drop duplicates ONLY among certain columns?
pass the column(s) to drop_duplicates
49
(Pandas) How would you identify duplicates ONLY among certain columns?
pass the column(s) to duplicated
50
(Pandas) How do you get drop_duplicates/duplicated to drop/flag ALL duplicate observations (vs all but one duplicate observation)?
by passing keep=False
51
(Pandas) In a DataFrame df, how would you change column x to 0, ONLY when pos is RB?  
df.loc[df['pos'] == 'RB', 'x'] = 0
52
(Pandas) How would you filter to only the instances where pos == 'RB' in a DataFrame df using query?
df.query("pos == 'RB'")