Skip to content
pythonadvanced

Python Data Science

pandas, numpy, matplotlib basics

7 questions

By EZ4Code Team

1. In numpy, which of the following creates a 3x3 zero matrix?

import numpy as np
np.zeros((3, 3))
np.zeros((3, 3))
np.zero(3, 3)
np.empty([3])
np.array(0, 3, 3)
Explanation: np.zeros(shape) takes a tuple representing the shape; (3,3) means a 3-row, 3-column all-zero array.

2. In pandas, which method is used to group by a column and aggregate?

groupby
merge
concat
pivot
Explanation: DataFrame.groupby(by) groups by column, usually combined with .agg()/.sum()/.mean() etc. for aggregation.

3. What does the following code output? import numpy as np a = np.array([1, 2, 3]) print(a * 2)

import numpy as np
a = np.array([1, 2, 3])
print(a * 2)
[2 4 6]
[1 2 3 1 2 3]
Error
[2 2 2]
Explanation: numpy supports broadcasting; multiplying a scalar with an array multiplies element-wise, resulting in [2 4 6].

4. What is the common function in pandas to read CSV files?

pd.read_csv()
pd.load_csv()
pd.open_csv()
pd.import_csv()
Explanation: pandas provides pd.read_csv() to read CSV files and return a DataFrame, supporting various parameters to customize parsing behavior.

5. What is the most commonly used function in matplotlib to plot a line chart?

plt.plot()
plt.bar()
plt.scatter()
plt.hist()
Explanation: plt.plot() is used for line charts; plt.bar() for bar charts, plt.scatter() for scatter plots, plt.hist() for histograms.

6. Regarding pandas DataFrame's loc and iloc, which is correct?

loc is label-based, iloc is integer-position-based
loc is integer-based, iloc is label-based
They are completely identical
Neither supports slicing
Explanation: loc accesses by labels (index names/column names); iloc accesses by integer positions. Both support slicing but with different meanings.

7. To handle missing values NaN, which method can delete rows containing NaN?

df.dropna()
df.fillna()
df.isna()
df.drop()
Explanation: dropna() deletes rows/columns containing missing values; fillna() fills missing values; isna() returns a boolean mask.

More python Quizzes