Session 3 - Sample Exam Qs Flashcards

1
Q

import pandas as pd
data = pd.read_csv(“/content/sample_data/california_housing_test.csv”)
print(data.head())

This code aims to read a CSV file using pandas.

What is the purpose of the head() method, and why is it used in this context? - (2)

A

The purpose of the head() method in pandas is to display the first few rows of the DataFrame.

It provides a quick overview of the data structure and its contents, helping users understand the data better.

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

Q1:
import pandas as pd
df = pd.read_csv(“content/sample_data/california_housing_test.csv”)
print(df.head())

Something is wrong with the following code. Explain what is wrong - and why.

A

The path name is missing a leading slash to indicate that it starts at the root.

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

To convert from celsius to fahrenheit you multiply by 9/5 and add 32. What is wrong with the following code? How might you fix it?

Note - there are two potential issues here - try to make the conversion as accurate as possible. - (2)

A

First, the list is made of strings but you are trying to multiply them by a number.

Second, you append ‘C’ not ‘F’ to the list so you never store the Fahrenheit value –> So, The code indeed appends the original Celsius value (C) to the list instead of the calculated Fahrenheit value (F), so it doesn’t store the converted Fahrenheit temperatures.

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

Password hell. What is the matter with this code?

A

The input statement is not in the loop so if you get it wrong once, you enter an infinite loop.

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

s = “Python_Data_Science”
words = s.split(‘-‘)
print(words)

I’m trying to split the string ‘s’ into individual words. What is going wrong?

A

It’s splitting on a hyphen (-) but the string has underscores in it (_)

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

Q3.5
I’m trying to read in a file using the Python csv reader. I think I almost have it but it doesn’t quite work…

Can you see what is wrong?

A

You should parse each row in csv_reader. csv_file does not exist

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