Pandas Pt 2 (UCSD) Flashcards

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

replace all of the cells in a dataframe that have value 9999 with 0

A

df = df.replace(9999, 0)

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

fill missing values in a dataframe with the last known value before it, or after it

A

df. fillna(method=’ffill’) ##forward

df. fillna(method=’backfill’) ##backward

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

drop rows or columns with any NaN values

A

df. dropna(axis=0) ## rows

df. dropna(axix=1) ## columns

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

interperolate missing values. default is linear interpolation

A

df.interpolate() ## fills in missing values using a linear interpolation, but there are others

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

create a dataframe with boolean values, where TRUE is set for any null values

A

df.isnull()

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

some common plot functions (but many more), df.plot.func()

A
funcs = bar(), box(), hist(), plot(), line(), pie(), scatter()
## would call differently in jupyter (w/o .plot)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

use a magic function in jupyter to use matplotlib in jupyter

A

%matplotlib inline

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

get the histogram of the ratings column of the df dataframe in jupyter notebook

A

df.hist( column = ‘ratings’, figsize = (15,10) ) ## figsize is the size that will be plotted in the notebook

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

get the boxplot of the ratings column of the df dataframe in jupyter notebook

A

df.boxplot( column = ‘ratings’, figsize = (15,10) ) ## figsize is the size that will be plotted in the notebook

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

return all of the rows from a dataframe where ‘col2’ values are greater than 5

A

df[ df[‘col2’ > 5]

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

delete rows indexed 5 and 6 in a dataframe

A

df.drop[ df.index[ 5,6 ] ] or df.drop[ ‘rowName’, row2name’ ]

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

delete column ‘col2’ from a dataframe

A

del df [ ‘col2’ ]

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

get the mean of rows aggregated on ‘studentID’

A

df.groupby(‘studentID’).mean() ## groupby aggregates the rows on the column specified

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

get the count of unique rows from a set of columns in a dataframe (each different permutation of columns is unique)

A

df[ [ list of cols ] ].value_counts() ## optional list of columns; otherwise entire df. gives the unique col permutations and the frequency of each

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