Python (UBC) Flashcards

1
Q

What function calls Pandas and what is Pandas typically abbreviated as?

A

> > > import pandas as pd

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

What function converts a .csv into a Pandas Dataframe? Use Dingo.csv in the example code

A

> > > Dingo = pd.read_csv(‘Dingo.csv’)

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

How would you make python show the dataframe ‘Dingo’? (Assume ‘Dingo’ is defined). How many observations of this dataframe are shown as default?

A

> > > Dingo
(inputting the object’s name will call it).

assuming Dingo contains more than 10 observations, Python will show the first 5 and last 5 of the data set (unless otherwise instructed).

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

Which of distinguish which of the following code snippets is an object, an attribute, a function, and an argument:
»> Dingo = pd.read_csv(‘Dingo.csv’)
»> Dingo.shape

A
# Dingo = pd.read_csv('Dingo.csv')
# Dingo.shape

Dingo is an object in both cases, pd.read_csv is a function (tell-tale sign is the brackets), ‘Dingo.csv’, is an argument, and shape is an attribute.

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

What attribute would you use to find out the variables of the dataframe ‘Dingo’. Provide the code required to answer the question

A

> > > Dingo.columns

N.B. Vertical columns are variables, Horizontal rows are observations

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

Let’s assume Dingo.shape returns (30, 5). What function is would allow us to look at the first five variables, of only the observations 10-19? Assume that the fifth variable is ‘location’

A

> > > Dingo.loc[10:19, :’location’]

N.B. df.loc[‘x start’:’x end’, ‘y start’:’y end’] and that not stating a start or end tells Python to use the terminus of the data. I.e. […, :’location’] is all columns left of the location as well as the ‘location’ column

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

Let’s assume Dingo.shape returns (30, 5), and that one variable is ‘location’. What are three ways we could display only the ‘location’ column?

A

> > > Dingo.loc[:, [‘location’]]

> > > Dingo.iloc[:, [5]]

> > > Dingo[[‘location’]

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

Let’s assume Dingo.shape returns (30, 5), and that one variable is ‘Nutterness’ (a percentile stored as a float). What function would allow us to sort from most Nutter to least Nutter within this dataframe

A

> > > Dingo.sorted_values(by=’Nutterness’, ascending=False)

N.B. that the sorted_values attribute defaults to ascending

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

What function would you use to show the basics statistics of a dataframe? Provide the code required to answer the question using the Dingo dataframe

A

> > > Dingo.describe()

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

What function would you use to show the basics statistics AND the categorical summaries of a dataframe? Provide the code required to answer the question using the Dingo dataframe

A

> > > Dingo.describe(include=’all’)

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

What’s the attribute for displaying the count or frequency of an object?

A

> > > object.value_counts()

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

Why is the following example incorrect?
»> Dingo_location = Dingo[[‘location’]]
»> Dingo_location.value_counts()

Assume the Dingo is defined, and contains the column ‘location’

A

For whatever reason the value_counts function can’t pair with the column lookup with two square brackets ‘[[…]]’, instead it uses a single, so it should look like this:

> > > Dingo_location = Dingo[‘location’]
Dingo_location.value_counts()

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

What is the function to export a csv?

A

> > > Dataframe.to_csv(‘desired filename’, index=False)

N.B. That Index=False omits the index column from being included in the csv

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