pandas 1_4 Flashcards

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?

A

str

24
Q

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

A

|

25
Q

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

A

&

26
Q

(Pandas) How do you negate a boolean column in Pandas?

A

~

27
Q

(Pandas) What is the method you use to call a function on every value in a column?

A

apply

28
Q

(Pandas) What does apply take as an argument?

A

a function you want to call on every observation in a column in your DataFrame

29
Q

(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?

A

axis=1

30
Q

(Pandas) What keyword argument would you pass the rename function to rename a column ‘old’ to ‘new’?

A

columns={‘old’: ‘new’}<br></br>

31
Q

(Pandas) What are missing values in Pandas?

A

np.nan

32
Q

(Pandas) What is the DataFrame/Series method that returns True if a value is missing, False if not?

A

isnull()

33
Q

(Pandas) What is the DataFrame/Series method that returns True if a value is NOT missing, False if not?

A

notnull()

34
Q

(Pandas) What is the method to replace np.nan with values of your choice?

A

filla()

35
Q

(Pandas) What method lets you convert between column types?

A

astype()

36
Q

(Pandas) What attribute lets you view the types of all your columns in a DataFrame?

A

dtypes

37
Q

(Pandas) What does mean(axis=1) calculate the average over?

A

rows

38
Q

(Pandas) What is the function to test whether any value of a boolean column is True?

A

any

39
Q

(Pandas) What is the function to test whether all values of a boolean column are True?

A

all

40
Q

(Pandas) How would you get the frequency of values for df[‘pos’] in Pandas?

A

df[‘pos’].value_counts()

41
Q

(Pandas) Given some list of index values, how would you make a DataFrame of only those values?

A

pass them to loc

42
Q

(Pandas) What is the second (optional) argument to loc?

A

column(s) you want to include

43
Q

(Pandas) When you pass a (similarly index) boolean column to loc, what will it return?

A

only the rows where the boolean column is True

44
Q

(Pandas) What does boolean indexing require re: the index of your DataFrame and the boolean column you’re working with?

A

that they have the same index

45
Q

(Pandas) How would you filter to only the instances where pos == ‘RB’ in a DataFrame df using loc?

A

df.loc[df[‘pos’] == ‘RB’]

46
Q

(Pandas) What is the DataFrame method to drop duplicates?

A

drop_duplicates

47
Q

(Pandas) What is the function to return a boolean column indicating whether a row is a duplicate?

A

duplicated

48
Q

(Pandas) How would you drop duplicates ONLY among certain columns?

A

pass the column(s) to drop_duplicates

49
Q

(Pandas) How would you identify duplicates ONLY among certain columns?

A

pass the column(s) to duplicated

50
Q

(Pandas) How do you get drop_duplicates/duplicated to drop/flag ALL duplicate observations (vs all but one duplicate observation)?

A

by passing keep=False

51
Q

(Pandas) In a DataFrame df, how would you change column x to 0, ONLY when pos is RB?

A

df.loc[df[‘pos’] == ‘RB’, ‘x’] = 0

52
Q

(Pandas) How would you filter to only the instances where pos == ‘RB’ in a DataFrame df using query?

A

df.query(“pos == ‘RB’”)