Lesson 10-19 Flashcards
True or False: A try block can be used to detect and handle a file not found error, when trying to open a file for reading.
True
True or False: The open() function can be used in order to read as well as write to files.
True
True or False: The open() function will read the contents of a file into a variable.
True
True or False: The readline() method will return an empty string when there is no more data to read from a file.
True
True or False: The string split() method only recognizes the comma as a delimiter
False
True or False: The string split() method returns a string result.
True
True or False: The result of the open function can be used as part of a for statement to loop over and read each line of a file.
True
True or False: The statements in an except block will get executed.
False
Which of the following methods can be used to read a line of a file into a string variable?
A. getline()
B.readline()
C.open()
D. All the above
B. readline()
True or False: In order to read a file in Python, the open() function can be used to connect a variable with the file.
True
A function that can be used to write data to a file is?
A. write()
B.print()
C.save()
B.print()
After all lines of a file have been read, what will a subsequent call to readline() return?
A.\n
B.’’ (empty string)
C.0
D.an error
B.”(empty string)
Before writing to a text file, what function should be called, to associate a variable with the file?
A. input()
B.print()
C.open()
D.write()
C. open()
The primary Python keywords for detecting and handling an exception are:
A. try, except
B. if, else
C. for, while
D. input, print
A. try, except
After using the open() function, which method can be used to read a line from a text file?
A.readtext()
B.readline()
C.readlin()
D.readnext()
B. readline()
The string returned by the readline() method includes which character at the end of the returned string?
A.\n
B.\t
C.double backspace
D.single backspace
A.\n
Which function can be used to write items to a text file?
A.print()
B.input()
C.write.Line()
D.save()
A.print()
After all lines have been written to a file, what method should be called, to ensure all lines have been saved?
A.Open()
B.Close()
C.Save()
D.end()
B. close()
Which statement can be used to associate a text file with a Python variable for subsequent reading?
A. f=open()
B.f=open(‘r’,’input.txt’)
C.f=open(‘input.txt’,’r’)
D.’input.txt’.open()
C.
The print() named parameter for specifying what to place between each item is:
A. delim
B. sep
C. csv
D. fieldsep
B. sep
The string split() method returns what data type?
A. Series
B. DataFrame
C. List
D. A string
List
An example where Python will generate an exception is:
A.The last item returned from range()
B.A call to open() to read a file that does not exist
C.An infinite loop
D.The use of break in a repeition body
B.
The pandas function that can read a comma separated file’s data into a DataFrame is:
A.load_data()
B.read_csv()
C.read_data()
B. read_csv()
True or False: When reading data from a file into a DataFrame, the data must be separated by commas.
False
True or False: The head() and tail() methods can only show one row of a DataFrame’s data.
False
Which can be used to determine the number of columns in a DataFrame? (select the best, most complete, answer)
A. the len() function
B.the shape property
C.the info() method
D. All the above
D. All the above
The pandas type for storing tabular data is called a:
DataFrame
True or False: Python has a built-in table data structure
False
True or False: Values in pandas Series cells are limited to int, float, and str types.
False
What type of result is returned by the DataFrame head() method?
A.DataFrame
B.Series
C.Method
D.List
A. DataFrame
What does the DataFrame shape property indicate?
The number of rows and columns of the DataFrame
If df is a DataFrame, what does len(df) return?
The number of rows of df
One approach to determine the number of missing values in a DataFrame is to:
A. the info() method
B.use the missing method
C.use for repetition and count the number of None values
D.find the total of each column
A. the info() method
True or False: Each DataFrame column optionally has a name
False
What DataFrame method returns its last rows?
tail()
In order to use pandas in your program, you need to use the ___ statement.
import
True or False: For efficiency, pandas uses the Python float type to represent numbers.
False, the correct answer is “NumPy float64” instead of “float”
What pandas function can be used to read a tabular text file and return a DataFrame?
read_csv
True or False:Pandas main focus is on non-tabular data.
False (pandas provides tools for collecting, transforming, and analyzing tabular data)
Which DataFrame method returns its first rows?
head()
True or False: The pandas read_csv() function requires files to in CSV (comma-separated value) format.
False (read_csv() can be used to read files with data separated by commas or other delimiters such as spaces or tabs)
In order to sort a DataFrame without creating a new DataFrame, the parameter specified is:
inplace
The tolist() method returns:
A. a list from a series
B.a series from a list
C.a series from a DataFrame
D.None of these options
A. a list from a series
If df is a DataFrame, and ‘colname’ is one of its column names, the expression df[‘colname’] returns:
A series
If df is a DataFrame, and ‘colname1’ and ‘colname2’ are two of its column names, the expression df[[‘colname1’, ‘colname2’]] returns:
A DataFrame
The method to sort a DataFrame is:
sort_values()
If df is a DataFrame, and colname is one of its columns, which is a valid filter expression? (Select the best, most complete, answer)
A. df[df.colname==100]
B. df[df[‘colname’]==100]
C. None of the options
D. Both A & B
D.
If df is a DataFrame, and ‘rowname’ is a name of one of its rows, which expression returns a row of the DataFrame?
A. df.iloc[‘rowname’]
B. df.loc[‘rowname’]
C. df.rowname
D. None of the options
E. All of the options
B. df.loc[‘rowname’]
How can values in a pandas Series method be accessed as a Python list?
A.using the tolist() method
B.using the getlist() method
C.using the list constructor
D.any of these
A. using the tolist() method
For a DataFrame df, how can its first row be accessed?
A. df.iloc[0]
B. df[0]
C.df.loc[0]
D. any of these
A. df.iloc[0]
If df is a pandas DataFrame with a column named ‘Sales’, what type of result is returned by df[‘Sales’] ?
A Pandas series
What DataFrame method returns a sorted DataFrame?
sort_values()
For several methods, pandas provides the ____named parameter, to indicate whether a new DataFrame is returned, or an existing DataFrame is updated.
inplace
If df is a pandas DataFrame with a column named ‘Sales’, what expression is an example of a filter expression?
A. df[df.Sales > 100]
B. df.filter(‘Sales > 100’)
C. [df.Sales > 100]
D. filter(Sales > 100)
A.
For a DataFrame df with a column named ‘Sales’ and a row index named ‘2023’, what expression will access the Sales for 2023?
A.Either of these
B. df[‘Sales’][‘2023’]
C.df.loc[‘2023’][‘Sales’]
A.
How can one row of a DataFrame be accessed?
A. using the .loc[] accessor
B. using the column name
C. using curly braces
D. none of these
A. using the .loc[] accessor
Which DataFrame method can be used with for, to iterate over the rows of a DataFrame?
iterrows()
Suppose you have the following DataFrame and field names:
DataFrame name: studentGrades
Field/column Names: student name, quiz1, quiz2, exam1, exam2
Write the python pandas code that will give descriptive analytics (count, mean, standard deviation, min, max, quartiles) for each numerical series in the dataframe.
studentGrades.describe()
Given the following DataFrame and field/column names write the python pandas code that shows the number of reviews by taster.
DataFrame name: wineReviews
Field/column names in winReviews: country, description, designation, points, price, province, taster_name, title, variety, winery
Don’t define a variable name for this just write the code to show the output
Use bracket selection (not dot notation) to select the field names
wineReviews[‘taster_name’].value_counts()
What is the purpose of descriptive analytics in an analytics project?
A.To predict future outcomes
B.To prescribe actions based on data analysis
C.To summarize and understand what has happened or is happening
D.To transform and standardize data
C.
What is a reason for standardizing data?
A.To predict future outcomes accurately
B.Enable meaningful comparisons regardless of scale of the original data
C.To summarize data and understand trends
D.To identify outliers in the dataset
B.
What does the shape attribute of a DataFrame tell you?
The number of columns and rows in the Dataframe
What is the general syntax to use the describe() function on a column?
A.dataframe.describe(column_name)
B.column_name.describe()
C.describe(dataframe, column_name)
D.dataframe[‘column_name’].describe()
D.
What is the purpose of the describe() function?
Provide summary statistics of a DataFrame
What does the describe() method provide for a numerical column?
A.mean, median
B.minimum and maximum values
C.standard deviation and quartiles
D.All of the options
D.
What does the unique() function in pandas do?
Returns the unique values in a column
What does the head() function in pandas do?
Returns the first 5 rows of the DataFrame
What type of analytics tells us what is likely to happen next?
Predictive Analytics
How can you read an Excel file into a pandas DataFrame?
A.pd.read_excel(‘filename.xlsx’)
B.pd.read_csv(‘filename.csv’)
C.pd.read_file(‘filename’)
A.
How can you insert a new column into a specific position in a DataFrame?
a) df.insert()
b) df.add_column()
c) df.new_column()
d) df.column_insert()
a. df.insert()
What is the purpose of the tail() function in pandas?
To dislay the last few rows of data of the DataFrame
What does the shape function return for a DataFrame?
Total number of rows and columns