Unit 9: Handling data with pandas - Practice Quiz

ECAP776 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 What is pandas mainly used for in Python?

Introduction to pandas Easy
A. Computer network configuration
B. Data manipulation and analysis
C. Operating system development
D. Web page styling and layout

2 Which statement commonly imports pandas using its standard alias?

Introduction to pandas Easy
A. using pandas as py
B. include pandas as pd
C. import pandas as pd
D. import pandas as py

3 What is a pandas Series?

Series Easy
A. A two-dimensional labeled table
B. A one-dimensional labeled array
C. A function for reading files
D. A collection of Python modules

4 Which expression creates a pandas Series from a Python list?

Series Easy
A. pd.sort_values([10, 20, 30])
B. pd.Series([10, 20, 30])
C. pd.read_csv([10, 20, 30])
D. pd.DataFrame([10, 20, 30])

5 What does the index of a pandas Series contain?

Series Easy
A. Formulas for the Series values
B. Data types for all columns
C. Labels for the Series values
D. File paths for stored values

6 What is a pandas DataFrame?

DataFrame Easy
A. A text-file compression format
B. A one-dimensional labeled array
C. A Python loop structure
D. A two-dimensional labeled table

7 In a DataFrame, what do columns usually represent?

DataFrame Easy
A. Different variables or attributes
B. Different Python environments
C. Different file storage locations
D. Different program execution loops

8 Which expression creates a DataFrame from a dictionary named data?

DataFrame Easy
A. pd.TableSeries(data)
B. pd.readFrame(data)
C. pd.SeriesFrame(data)
D. pd.DataFrame(data)

9 What does the shape attribute of a DataFrame return?

DataFrame Easy
A. The number of rows and columns
B. The names of rows and columns
C. The first and last records
D. The minimum and maximum values

10 By default, what does df.head() display?

DataFrame Easy
A. The first five columns
B. The first five rows
C. The last five columns
D. The last five rows

11 Which DataFrame method sorts rows according to the values in a column?

Sorting Easy
A. sort_values()
B. reset_index()
C. value_counts()
D. sort_index()

12 Which argument sorts DataFrame values in descending order?

Sorting Easy
A. ascending=True
B. reverse=False
C. ascending=False
D. descending=False

13 Which method sorts a DataFrame according to its index labels?

Sorting Easy
A. set_index()
B. drop_index()
C. sort_index()
D. sort_values()

14 Which pandas function reads data from a CSV file?

Working with CSV files Easy
A. pd.import_csv()
B. pd.load_csv()
C. pd.open_csv()
D. pd.read_csv()

15 Which DataFrame method writes data to a CSV file?

Working with CSV files Easy
A. to_csv()
B. export_csv()
C. write_csv()
D. save_csv()

16 What is the purpose of index=False in df.to_csv("data.csv", index=False)?

Working with CSV files Easy
A. It excludes row labels from the file
B. It removes duplicate rows from the file
C. It excludes column names from the file
D. It sorts all rows before saving

17 If df is a DataFrame, what does df["Age"] usually return?

Operations using DataFrame Easy
A. The index named Age
B. The column named Age
C. The file named Age
D. The row named Age

18 Which statement creates a new column named Total by adding columns A and B?

Operations using DataFrame Easy
A. df("Total") = df("A") + df("B")
B. df["A"] = df["Total"] + df["B"]
C. df["Total"] = df["A"] + df["B"]
D. df.add("Total", "A", "B")

19 Which DataFrame method removes rows containing missing values by default?

Operations using DataFrame Easy
A. dropna()
B. fillna()
C. isna()
D. notna()

20 What does df.describe() commonly provide for numerical columns?

Operations using DataFrame Easy
A. Sorted index labels
B. Summary statistics
C. CSV file settings
D. Column data types only

21 Which statement correctly imports pandas using its conventional alias and creates a labeled one-dimensional object?

Introduction to pandas Medium
A. import pandas as pd; s = pd.Series([10, 20])
B. from pandas import pd; s = pd.Array([10, 20])
C. import pandas as pn; s = pd.DataFrame([10, 20])
D. import pandas; s = pandas.Table([10, 20])

22 Given s1 = pd.Series([10, 20], index=['a', 'b']) and s2 = pd.Series([1, 2], index=['b', 'c']), what is the result of s1 + s2?

Series Medium
A. A Series containing 10, 21, and 2 at indices a, b, and c
B. A Series containing 11 and 22 at indices a and b
C. A Series containing 21 only at index b
D. A Series containing NaN, 21, and NaN at indices a, b, and c

23 For s = pd.Series([8, 12, 16], index=['x', 'y', 'z']), which expression returns the first two values by position?

Series Medium
A. s.iloc[1:3]
B. s.loc['x':'x']
C. s.loc[0:1]
D. s.iloc[0:2]

24 What columns and number of rows are produced by pd.DataFrame({'name': ['Ana', 'Bo'], 'score': [80, 90]})?

DataFrame Medium
A. Columns Ana and Bo, with 2 rows
B. Columns name and score, with 4 rows
C. Columns name and score, with 2 rows
D. One unnamed column, with 4 rows

25 A DataFrame df contains columns name, age, and city. Which expression returns a DataFrame containing only name and city?

DataFrame Medium
A. df['name', 'city']
B. df.iloc['name', 'city']
C. df[['name', 'city']]
D. df.loc['name', 'city']

26 Which expression selects rows where score is at least 70 and attempts is less than 3?

Operations using DataFrame Medium
A. df[df['score'] >= 70 and df['attempts'] < 3]
B. df[(df['score'] >= 70) | (df['attempts'] < 3)]
C. df[df['score'] >= 70, df['attempts'] < 3]
D. df[(df['score'] >= 70) & (df['attempts'] < 3)]

27 A DataFrame has numeric columns price and quantity. Which statement creates a total column containing their row-wise products?

Operations using DataFrame Medium
A. df['total'] = df[['price', 'quantity']].product()
B. df['total'] = df['price'] * df['quantity']
C. df['total'] = df[['price', 'quantity']].sum()
D. df['total'] = df['price'] + df['quantity']

28 Which command sorts df by department in ascending order and then by salary in descending order?

Sorting Medium
A. df.sort_values(['salary', 'department'], ascending=[True, False])
B. df.sort_values(['department', 'salary'], ascending=[False, True])
C. df.sort_values(['department', 'salary'], ascending=[True, False])
D. df.sort_index(['department', 'salary'], ascending=[True, False])

29 After filtering, a DataFrame has row labels [7, 2, 5]. Which expression sorts the rows by these labels without changing column order?

Sorting Medium
A. df.sort_values()
B. df.sort_index(axis=1)
C. df.reset_index(drop=True)
D. df.sort_index(axis=0)

30 A file students.csv contains five columns. Which command reads only the name and grade columns?

Working with CSV files Medium
A. pd.read_csv('students.csv', usecols=['name', 'grade'])
B. pd.read_csv('students.csv', index_col=['name', 'grade'])
C. pd.read_csv('students.csv', columns=['name', 'grade'])
D. pd.read_csv('students.csv', names=['name', 'grade'])

31 The first column of sales.csv is named order_id and should become the DataFrame index. Which command does this while reading the file?

Working with CSV files Medium
A. pd.read_csv('sales.csv', index_col='order_id')
B. pd.read_csv('sales.csv', usecols='order_id')
C. pd.read_csv('sales.csv', names='order_id')
D. pd.read_csv('sales.csv', header='order_id')

32 A CSV column named date contains values such as 2026-09-10. Which command asks pandas to convert that column to date-time values during import?

Working with CSV files Medium
A. pd.read_csv('events.csv', parse_dates=['date'])
B. pd.read_csv('events.csv', convert_dates=['date'])
C. pd.read_csv('events.csv', date_col=['date'])
D. pd.read_csv('events.csv', dtype=['date'])

33 For a DataFrame containing only numeric values, what does df.mean(axis=1) calculate?

Operations using DataFrame Medium
A. The mean of all values as one number
B. The mean of each row
C. The mean of each column
D. The mean of the index labels

34 A DataFrame has columns department and salary. Which expression calculates the average salary for each department?

Operations using DataFrame Medium
A. df.groupby('salary')['department'].mean()
B. df['salary'].groupby(df.index).sum()
C. df.groupby('department')['salary'].mean()
D. df.groupby('department')['salary'].count()

35 Which expression removes only rows where the email value is missing, regardless of missing values in other columns?

Operations using DataFrame Medium
A. df.dropna(subset=['email'])
B. df.drop(columns=['email'])
C. df.dropna()
D. df.dropna(axis=1)

36 Which statement replaces missing values in the score column with that column's median while leaving other columns unchanged?

Operations using DataFrame Medium
A. df = df['score'].dropna(df['score'].median())
B. df['score'] = df.fillna(df['score'].median())
C. df['score'] = df['score'].fillna(df['score'].median())
D. df['score'] = df['score'].replace(df['score'].mean())

37 Which expression renames the column old_name to new_name without renaming any index labels?

DataFrame Medium
A. df.replace({'old_name': 'new_name'})
B. df.rename(index={'old_name': 'new_name'})
C. df.columns.rename({'old_name': 'new_name'})
D. df.rename(columns={'old_name': 'new_name'})

38 DataFrames customers and orders both contain customer_id. Which expression keeps only IDs present in both DataFrames?

Operations using DataFrame Medium
A. customers.join(orders, how='outer')
B. pd.merge(customers, orders, on='customer_id', how='outer')
C. pd.merge(customers, orders, on='customer_id', how='inner')
D. pd.concat([customers, orders], axis=0)

39 A Series colors contains ['red', 'blue', 'red', 'green', 'blue', 'red']. What does colors.value_counts() return first by default?

Series Medium
A. The label green with count 1
B. The label red with count 3
C. The label red with count 1
D. The label blue with count 2

40 Two DataFrames jan and feb have identical columns but overlapping index labels. Which expression stacks their rows and creates a fresh sequential index?

Operations using DataFrame Medium
A. pd.concat([jan, feb], axis=0, ignore_index=True)
B. pd.merge(jan, feb, how='inner', ignore_index=True)
C. pd.concat([jan, feb], axis=1, ignore_index=True)
D. jan.join(feb, axis=0, ignore_index=True)

41 Given:

left = pd.Series([10, 20], index=["a", "b"])

right = pd.Series([1, 2], index=["b", "c"])

What is (left + right).fillna(0).to_dict()?

Introduction to pandas Hard
A. {"a": 11.0, "b": 22.0, "c": 2.0}
B. {"a": 0.0, "b": 21.0, "c": 0.0}
C. {"a": 10.0, "b": 21.0, "c": 2.0}
D. {"a": 11.0, "b": 22.0, "c": 0.0}

42 Consider s = pd.Series([3, 1, 2], index=["x", "y", "x"]). Which statement about r = s["x"] is correct?

Series Hard
A. r is the scalar 3, because the first match is selected.
B. r is a Series whose values sum to 5.
C. r raises an error because index labels must be unique.
D. r is the scalar 2, because the last match is selected.

43 Given s = pd.Series([1, pd.NA, 3], dtype="Int64") and r = s * 2, which pair correctly describes r?

Series Hard
A. Its dtype is Int64, and its values are [2, 0, 6].
B. Its dtype is object, and its values are [2, None, 6].
C. Its dtype is Int64, and its values are [2, <NA>, 6].
D. Its dtype is float64, and its values are [2.0, NaN, 6.0].

44 Given:

p = pd.Series([1, 2], index=["a", "b"])

q = pd.Series([3, 4], index=["b", "c"])

df = pd.DataFrame({"p": p, "q": q})

Which result is correct?

DataFrame Hard
A. df.shape == (3, 2) and df.loc["b"].sum() == 5
B. df.shape == (3, 2) and df.loc["b"].sum() == 6
C. df.shape == (2, 2) and df.loc["b"].sum() == 5
D. df.shape == (4, 2) and df.loc["b"].sum() == 5

45 Given:

df = pd.DataFrame({"a": [1, 2], "b": [3, 4]}, index=["x", "y"])

df.loc[:, ["a", "b"]] = df.loc[:, ["b", "a"]]

What are the final columns a and b?

DataFrame Hard
A. a is [3, 2], and b is [1, 4].
B. a is [1, 2], and b is [3, 4].
C. a is [3, 4], and b is [1, 2].
D. a is [1, 4], and b is [3, 2].

46 Given:

df = pd.DataFrame({"v": [10, 20, 30]}, index=["a", "b", "c"])

mask = pd.Series([True, False, True], index=["c", "a", "b"])

What is df.loc[mask].index.tolist()?

DataFrame Hard
A. ["b", "c"]
B. ["c", "b"]
C. ["a", "c"]
D. ["c", "a"]

47 For the DataFrame below, what is the resulting id order after df.sort_values(["a", "b"], ascending=[True, False], na_position="first")?

df = pd.DataFrame({"id": [0, 1, 2, 3], "a": [2, 1, 1, np.nan], "b": [1, 2, 1, 0]})

Sorting Hard
A. [0, 1, 2, 3]
B. [1, 2, 0, 3]
C. [3, 1, 2, 0]
D. [3, 2, 1, 0]

48 Given:

df = pd.DataFrame({"id": [0, 1, 2], "a": [-2, 1, -1], "b": [1, -3, 2]})

What is the id order produced by df.sort_values(["a", "b"], key=lambda s: s.abs())?

Sorting Hard
A. [0, 2, 1]
B. [2, 0, 1]
C. [1, 2, 0]
D. [2, 1, 0]

49 A DataFrame must be sorted by one column while preserving the original relative order of rows with equal keys. Which call explicitly provides that stability?

Sorting Hard
A. df.sort_values("k", kind="heapsort")
B. df.sort_values("k", kind="quicksort")
C. df.sort_values("k", kind="mergesort")
D. df.sort_values("k", na_position="first")

50 A CSV contains:

id,value\n001,NA\n002,\n003,null\n

Which call preserves the IDs' leading zeros and reads all three value fields as literal strings rather than missing values?

Working with CSV files Hard
A. pd.read_csv(path, dtype=str, na_filter=True)
B. pd.read_csv(path, dtype={"id": int, "value": "string"}, na_filter=False)
C. pd.read_csv(path, dtype={"id": "string"}, keep_default_na=True)
D. pd.read_csv(path, dtype={"id": "string", "value": "string"}, keep_default_na=False)

51 A CSV has columns in the order a,b,c. What column order is normally produced by pd.read_csv(path, usecols=["c", "a"])?

Working with CSV files Hard
A. ["c", "b", "a"], reversing the CSV order
B. ["a", "c"], matching the CSV file order
C. ["c", "a"], matching the usecols list
D. ["a", "b", "c"], retaining every CSV column

52 Suppose chunks is a list of DataFrames read from one CSV using chunksize, and chunk sizes differ. Which expression computes the exact overall mean of numeric column x, ignoring missing values?

Working with CSV files Hard
A. sum(c["x"].sum() for c in chunks) / sum(c["x"].count() for c in chunks)
B. sum(c["x"].sum() for c in chunks) / len(chunks)
C. sum(c["x"].mean() for c in chunks) / sum(c["x"].count() for c in chunks)
D. sum(c["x"].mean() for c in chunks) / len(chunks)

53 Given the CSV text a,b\n1,10\n2,20\n3,30\n, what is the sum of column a after calling pd.read_csv(io.StringIO(text), skiprows=lambda i: i == 2)?

Working with CSV files Hard
A. 3
B. 6
C. 4
D. 5

54 Given:

df = pd.DataFrame({"x": [1, 2], "y": [10, 20]}, index=["a", "b"])

s = pd.Series([100, 200], index=["b", "a"])

r = df.add(s, axis="index")

What is r.loc["a"].tolist() + r.loc["b"].tolist()?

Operations using DataFrame Hard
A. [101, 210, 202, 120]
B. [201, 210, 102, 120]
C. [101, 110, 202, 220]
D. [201, 110, 102, 220]

55 A left DataFrame has join keys [1, 1, 2], while the right DataFrame has unique join keys [1, 2]. What is the most restrictive validate mode that permits left.merge(right, on="key", validate=...)?

Operations using DataFrame Hard
A. "many_to_many"
B. "one_to_one"
C. "many_to_one"
D. "one_to_many"

56 Given:

a = pd.DataFrame({"x": [1, 2]}, index=["u", "v"])

b = pd.DataFrame({"y": [3, 4]}, index=["v", "w"])

What describes pd.concat([a, b], axis=1, join="inner")?

Operations using DataFrame Hard
A. It has index ["v"] and values x=2, y=3 at v.
B. It has index ["v"] and values x=1, y=4 at v.
C. It has index ["u", "v", "w"] and values x=2, y=3 at v.
D. It has index ["u", "w"] and no row labeled v.

57 Given:

df = pd.DataFrame({"g": ["A", "A", "B"], "x": [1, 3, 10]})

What is df[df["x"] > df.groupby("g")["x"].transform("mean")]["x"].tolist()?

Operations using DataFrame Hard
A. [10]
B. [3]
C. [1, 3]
D. [3, 10]

58 A DataFrame contains two rows with the same id and key pair but values 10 and 20. What happens when these columns are reshaped with df.pivot(index="id", columns="key", values="value")?

Operations using DataFrame Hard
A. The values are averaged, producing 15.
B. A ValueError is raised for duplicate entries.
C. The first value is retained, producing 10.
D. The last value is retained, producing 20.

59 Given df = pd.DataFrame({"a": [1, np.nan], "b": [np.nan, np.nan]}), what does df.sum(min_count=1) contain?

Operations using DataFrame Hard
A. a is 0.0, and b is 0.0.
B. a is NaN, and b is NaN.
C. a is 1.0, and b is NaN.
D. a is 1.0, and b is 0.0.

60 Given:

df = pd.DataFrame({"a": [1, 2], "b": [3, 4]}, index=["x", "y"])

other = pd.DataFrame({"a": [9, 8], "b": [np.nan, 7]}, index=["y", "z"])

After df.update(other), what is df.loc["y"].tolist()?

Operations using DataFrame Hard
A. [2, 4]
B. [9, NaN]
C. [9, 4]
D. [8, 7]