Neural Sync Active
Week 12: NumPy & Pandas Foundations
Registry Synced
Week 12: NumPy & Pandas Foundations
4526 words
23 min read
2026-09-12
Reading compass
Now · TL;DR
Programming in Python — Week 12: NumPy & Pandas Foundations
The official IITM syllabus concludes with Week 12: Basic Pandas and NumPy Processing. The syllabus file gives only this broad heading, so the detailed material below is a standard introductory expansion of those tools, not a claim about the exact lecture ordering.
TL;DR
Until now, you processed data mostly with:
pythonfor x in values: ...
NumPy and Pandas provide higher-level ways to work with many values at once.
textPython lists ↓ NumPy arrays ↓ fast numerical operations tabular records ↓ Pandas DataFrame ↓ filter / select / aggregate
Core distinction:
1. Why NumPy?
Suppose:
pythonvalues = [1, 2, 3, 4]
To double every value manually:
pythonresult = [] for x in values: result.append(2 * x)
With NumPy:
pythonimport numpy as np a = np.array([1, 2, 3, 4]) result = 2 * a
Conceptually:
text[1,2,3,4] × 2 ↓ [2,4,6,8]
The operation is expressed directly over the entire array.
2. Import convention
NumPy is conventionally imported as:
pythonimport numpy as np
Pandas is conventionally:
pythonimport pandas as pd
So later you commonly see:
pythonnp.array(...) pd.DataFrame(...)
3. NumPy array
A NumPy array stores numerical data in an array structure.
pythona = np.array([10, 20, 30])
Conceptually:
textindex: 0 1 2 value: 10 20 30
Like a list, it supports indexing.
4. Array indexing
pythona = np.array([10, 20, 30]) print(a[0]) print(a[2])
Outputs:
text10 30
Zero-based indexing remains.
5. Negative indexing
pythona[-1]
returns the last element.
So familiar Python sequence ideas continue to apply.
6. NumPy array vs Python list
A NumPy array is not simply another spelling for a list.
Broadly:
| Feature | Python list | NumPy array |
|---|---|---|
| General-purpose values | Yes | Mainly homogeneous numerical data |
| Element-wise numeric operations | Usually manual | Natural/vectorized |
| Multidimensional numeric data | Nested lists | Native array structure |
| Numerical library operations | Limited | Extensive |
7. Element-wise arithmetic
pythona = np.array([1, 2, 3]) print(a + 10)
Result:
text[11 12 13]
Each element receives the operation.
Mathematically:
8. Multiplication
pythona * 2
gives:
text[2 4 6]
Compare Python list:
python[1, 2, 3] * 2
which gives repetition:
python[1, 2, 3, 1, 2, 3]
This difference is important.
9. Array-to-array arithmetic
pythona = np.array([1, 2, 3]) b = np.array([10, 20, 30]) print(a + b)
Result:
text[11 22 33]
Element-wise:
10. Other element-wise operations
pythona - b a * b a / b a ** 2
operate element-by-element when shapes are compatible.
11. Vectorization
Writing:
pythona * 2
instead of manually looping over every element is often called vectorized computation.
Conceptually:
textmanual: for each x: x * 2 vectorized: array * 2
The second expresses the computation at a higher level.
12. Creating arrays
From a list:
pythona = np.array([1, 2, 3])
Zeros:
pythonnp.zeros(5)
Conceptually:
text[0. 0. 0. 0. 0.]
Ones:
pythonnp.ones(4)
13. np.arange()
NumPy provides:
pythonnp.arange(...)
which resembles Python's
range() but returns an array.Example:
pythonnp.arange(5)
conceptually gives:
text[0 1 2 3 4]
14. Start, stop, step
pythonnp.arange(2, 10, 2)
gives:
text[2 4 6 8]
Like
range():15. Array shape
NumPy arrays have a:
pythona.shape
property.
For:
pythona = np.array([10, 20, 30])
shape is conceptually:
text(3,)
meaning one dimension with 3 elements.
16. Two-dimensional array
pythona = np.array([ [1, 2, 3], [4, 5, 6] ])
This has:
rows and:
columns.
Shape:
text(2, 3)
17. Matrix-style indexing
For:
pythona = np.array([ [1, 2, 3], [4, 5, 6] ])
you can access:
pythona[0, 1]
which gives:
text2
Row 0, column 1.
18. Contrast with nested list syntax
Nested Python list:
pythonx[0][1]
NumPy commonly supports:
pythona[0, 1]
This directly expresses multidimensional indexing.
19. Slicing arrays
pythona = np.array([10, 20, 30, 40, 50]) a[1:4]
gives:
text[20 30 40]
Same half-open slicing:
20. Slice a row
For matrix-like array:
pythona[0, :]
means:
row 0, all columns.
Conceptually:
text[1 2 3]
21. Slice a column
pythona[:, 1]
means:
all rows, column 1.
For:
text1 2 3 4 5 6
result:
text[2 5]
22. Colon :
In array indexing:
python:
roughly means:
take the full range along this dimension.
So:
pythona[:, 0]
means every row, first column.
23. Array dimensions
Useful properties include:
pythona.ndim a.shape a.size
Conceptually:
ndim→ number of dimensionsshape→ size along each dimensionsize→ total number of elements
24. Example
For:
pythona = np.array([ [1, 2, 3], [4, 5, 6] ])
we have conceptually:
textndim = 2 shape = (2,3) size = 6
25. Reshaping
An array with 6 elements:
pythona = np.array([1, 2, 3, 4, 5, 6])
can conceptually be reshaped:
pythona.reshape(2, 3)
into:
text1 2 3 4 5 6
The total number of elements must remain consistent.
26. Shape compatibility
You cannot reshape 6 elements into:
text4 × 2
because:
Always check:
27. NumPy aggregation
NumPy provides operations such as:
pythonnp.sum(a) np.mean(a) np.min(a) np.max(a)
These summarize arrays.
28. Example
pythona = np.array([2, 4, 6, 8]) np.sum(a)
gives:
pythonnp.mean(a)
gives:
29. Axis intuition
For two-dimensional data:
text1 2 3 4 5 6
you may aggregate across different dimensions.
Conceptually:
- row-wise
- column-wise
- whole array
NumPy uses an
axis argument for such operations.30. Whole-array sum
pythona.sum()
for:
text1 2 3 4 5 6
gives:
31. Column-style aggregation
For a matrix, an axis operation can produce totals such as:
The key conceptual lesson is:
an axis tells NumPy which dimension to collapse during aggregation.
32. Boolean comparisons on arrays
pythona = np.array([10, 20, 30]) a > 15
gives a Boolean array:
text[False True True]
Comparison happens element-wise.
33. Boolean masking
You can use that Boolean result to filter an array:
pythona[a > 15]
Result:
text[20 30]
This is one of the most important NumPy patterns.
34. Mental expansion of mask
pythona[a > 15]
means conceptually:
pythonresult = [] for x in a: if x > 15: result.append(x)
NumPy expresses the whole operation compactly.
35. Multiple conditions
Conceptually, to select values satisfying both conditions:
and:
NumPy uses element-wise Boolean operations.
A common form is:
pythona[(a > 10) & (a < 30)]
Parentheses matter.
36. Why not ordinary and?
For array-wide element-wise comparison, NumPy commonly uses:
python& |
with Boolean arrays rather than Python's scalar-style
and / or.This is an important distinction when moving from single values to vectorized arrays.
37. NumPy datatype
Arrays have a datatype:
pythona.dtype
A numerical array may contain:
- integers
- floating-point numbers
- other fixed numerical types
NumPy usually works most naturally with homogeneous data.
38. Automatic conversion
If you create:
pythonnp.array([1, 2, 3.5])
NumPy may promote values into a common compatible type, such as floating point.
So conceptually the array may become:
text[1.0, 2.0, 3.5]
39. Why homogeneous types help
When every element follows a common representation, NumPy can process large blocks of numerical data efficiently.
This differs from general Python lists, which may contain arbitrary mixtures of objects.
40. NumPy missing-value intuition
Numerical data sometimes contains missing values represented with concepts such as:
pythonnp.nan
NaN broadly means:not a valid ordinary numerical value / missing numerical value.
Detailed missing-data treatment becomes especially relevant with Pandas.
41. Pandas
Pandas is a library for working with labelled and tabular data.
Its two famous structures are:
textSeries DataFrame
For this course, the DataFrame is especially important.
42. DataFrame
A DataFrame can be thought of as a table:
| name | mark | city |
|---|---|---|
| A | 80 | X |
| B | 92 | Y |
| C | 70 | X |
Rows are records.
Columns are variables/fields.
43. Create DataFrame
pythonimport pandas as pd df = pd.DataFrame({ "name": ["A", "B", "C"], "mark": [80, 92, 70], "city": ["X", "Y", "X"] })
Now
df represents a table.44. Dictionary-of-lists connection
Notice:
python{ "name": [...], "mark": [...], "city": [...] }
uses ideas from Weeks 6–8.
Pandas builds a richer table abstraction on top of familiar collection concepts.
45. Inspect DataFrame
Simply:
pythonprint(df)
shows a tabular representation.
Pandas also provides tools for quickly inspecting portions of a dataset.
46. .head()
pythondf.head()
shows the first few rows.
Useful for answering:
What does this dataset look like?
without printing everything.
47. .tail()
pythondf.tail()
shows the final few rows.
48. DataFrame shape
pythondf.shape
returns:
text(number_of_rows, number_of_columns)
For our 3-row, 3-column table:
text(3,3)
49. Column names
pythondf.columns
provides the DataFrame's column labels.
Conceptually:
textname mark city
50. Select a column
pythondf["mark"]
selects the
mark column.Conceptually:
text80 92 70
This usually produces a Pandas
Series.51. Series
A Series is roughly a one-dimensional labelled data structure.
Think:
textindex → value
For the
mark column:text0 → 80 1 → 92 2 → 70
52. Select multiple columns
pythondf["name", "mark"](/viewer?path="name", "mark")
returns another DataFrame containing only those columns.
Note the double brackets:
python["name", "mark"]
is itself a list of column names.
53. Filtering rows
Suppose:
pythondf["mark"] >= 80
produces conceptually:
textTrue True False
Then:
pythondf[df["mark"] >= 80]
returns only matching rows.
54. Filtering mental model
pythondf[df["mark"] >= 80]
corresponds conceptually to:
pythonresult = [] for row in rows: if row["mark"] >= 80: result.append(row)
Pandas provides a tabular, vectorized form.
55. Multiple conditions
Example:
pythondf[(df["mark"] >= 80) & (df["city"] == "X")]
This means:
rows where mark is at least 80 and city is X.
Again, parentheses matter.
56. Select a filtered column
You can combine row filtering and column selection.
Conceptually:
textfilter rows ↓ select desired field
For example, get names of high-scoring students from the filtered table.
57. Aggregation in Pandas
For one numerical column:
pythondf["mark"].mean() df["mark"].sum() df["mark"].min() df["mark"].max()
These are table-level versions of the same aggregation patterns you've already learned.
58. Example mean
For:
text80, 92, 70
mean:
Pandas can compute this directly:
pythondf["mark"].mean()
59. value_counts()
For categorical data:
pythondf["city"].value_counts()
conceptually builds a frequency table.
For:
textX Y X
result conceptually:
textX → 2 Y → 1
This is the Pandas equivalent of a frequency dictionary pattern.
60. Connection to manual dictionary frequency
Earlier:
pythonfreq = {} for x in values: freq[x] = freq.get(x, 0) + 1
Now:
pythonseries.value_counts()
encapsulates the same broad task.
61. Sorting DataFrame
Conceptually:
pythondf.sort_values("mark")
sorts rows by the
mark column.Descending:
pythondf.sort_values("mark", ascending=False)
62. Sorting keeps records together
This is important.
If one row is:
textB,92,Y
sorting by
mark moves the whole row.It does not sort the marks independently from names/cities.
This preserves row relationships.
63. Add a new column
pythondf["passed"] = df["mark"] >= 40
Now the table gains a Boolean column.
Conceptually:
| name | mark | passed |
|---|---|---|
| A | 80 | True |
| B | 92 | True |
| C | 70 | True |
64. Transform a column
Suppose:
pythondf["bonus_mark"] = df["mark"] + 5
The operation applies to the entire column.
Again, this is vectorized processing.
65. Column arithmetic
pythondf["percentage"] = ( df["obtained"] / df["total"] * 100 )
Pandas applies arithmetic row-by-row according to column alignment.
This replaces many explicit loops.
66. Read CSV
A major Pandas feature:
pythondf = pd.read_csv("data.csv")
This handles CSV parsing at a much higher level than manually:
pythonfor line in f: line.split(",")
67. Manual files vs Pandas
Weeks 9–10:
pythonwith open(...) as f: for line in f: ...
Week 12:
pythondf = pd.read_csv(...)
The lower-level skills still matter because they explain what the library is abstracting away.
68. CSV becomes table
File:
textname,mark A,80 B,92 C,70
Then:
pythondf = pd.read_csv("marks.csv")
conceptually produces:
| name | mark |
|---|---|
| A | 80 |
| B | 92 |
| C | 70 |
69. Write CSV
Pandas can also write tabular data:
pythondf.to_csv("output.csv", index=False)
The
index=False part commonly means:do not add the DataFrame's index as an extra CSV column.
70. DataFrame index
Pandas rows normally have an index.
Conceptually:
| index | name | mark |
|---|---|---|
| 0 | A | 80 |
| 1 | B | 92 |
| 2 | C | 70 |
The index acts as row labels.
It is not necessarily the same thing as a data column.
71. Index vs ordinary column
Suppose student ID is real data:
textstudent_id
That is not automatically the same thing as Pandas' internal row index.
Keep the conceptual distinction:
textindex = row label column = dataset variable
72. .loc
Pandas provides label-oriented selection using:
pythondf.loc[...]
Conceptually:
select rows/columns by labels/conditions.
Example style:
pythondf.loc[df["mark"] >= 80, ["name", "mark"]]
73. .iloc
Pandas also provides position-oriented selection:
pythondf.iloc[...]
Conceptually:
select by integer position.
This is closer to Python list/NumPy positional indexing.
74. loc vs iloc
Broad mental model:
textloc → labels iloc → integer positions
This distinction becomes important once DataFrame indices are not simply
0,1,2,....75. Example iloc
pythondf.iloc[0]
means:
first row by position.
pythondf.iloc[:, 1]
means:
all rows, second column by position.
76. Missing values
Real tabular data may contain missing values.
Pandas commonly represents missing numerical/tabular entries using values such as
NaN.Example conceptual table:
| name | mark |
|---|---|
| A | 80 |
| B | NaN |
| C | 70 |
77. Detect missing values
Pandas provides operations such as:
pythondf.isna()
or column-level:
pythondf["mark"].isna()
These create Boolean masks identifying missing entries.
78. Drop missing data
A common operation:
pythondf.dropna()
returns data with missing rows removed according to the chosen rules.
Do not treat dropping as automatically correct—it depends on the problem.
79. Fill missing data
Another operation:
pythondf.fillna(...)
replaces missing values with chosen replacements.
Again, choice of replacement depends on the data meaning.
80. Data cleaning
A basic Pandas workflow often involves:
textload ↓ inspect ↓ clean ↓ filter ↓ transform ↓ aggregate ↓ save
This is the higher-level version of the manual file pipeline from Weeks 9–10.
81. Data types in Pandas
Columns have data types.
Examples conceptually:
textmark → numeric name → text passed → Boolean
Wrong datatypes can prevent calculations.
For example, marks stored as strings cannot be meaningfully averaged without conversion.
82. Conversion intuition
Pandas provides mechanisms for converting columns into appropriate datatypes.
The important conceptual lesson:
This is the same idea you saw with:
pythonint(input())
and:
pythonfloat(...)
83. Grouping
Suppose:
| city | mark |
|---|---|
| X | 80 |
| Y | 92 |
| X | 70 |
A natural question:
What is average mark per city?
Manual approach:
pythonsum_by_city = {} count_by_city = {}
Pandas provides higher-level grouping operations.
84. groupby() intuition
Conceptually:
pythondf.groupby("city")
means:
partition rows by city.
Then aggregation can be applied to each group.
Conceptually:
textX → rows with X Y → rows with Y
85. Grouped mean
Conceptual Pandas form:
pythondf.groupby("city")["mark"].mean()
For data:
textX → 80,70 Y → 92
gives:
86. Connection to Week 7 dictionary grouping
Earlier manual pattern:
pythontotal[group] = total.get(group, 0) + value count[group] = count.get(group, 0) + 1
Pandas
groupby() expresses the same higher-level idea.87. NumPy ↔ Pandas connection
Pandas uses array-oriented ideas heavily.
A DataFrame column often behaves like an array-like labelled vector.
So:
textNumPy numerical arrays ↓ Pandas labelled/tabular structures
They complement rather than replace each other.
88. NumPy is lower-level numerically
Use NumPy naturally for:
- vectors
- matrices
- numerical transformations
- array arithmetic
- numerical algorithms
Use Pandas naturally for:
- CSV/tabular records
- named columns
- row filtering
- grouping
- data cleaning
89. Example complete Pandas pipeline
File:
textname,city,mark A,X,80 B,Y,35 C,X,90 D,Y,75
Code conceptually:
pythonimport pandas as pd df = pd.read_csv("marks.csv") passed = df[df["mark"] >= 40] average = passed["mark"].mean() passed.to_csv("passed.csv", index=False)
This performs:
textread → filter → aggregate → write
without manual file loops.
90. Equivalent manual pipeline
Weeks 9–10 version:
pythontotal = 0 count = 0 with open("marks.csv", "r") as src: with open("passed.csv", "w") as dst: header = src.readline() dst.write(header) for line in src: name, city, mark = line.strip().split(",") mark = int(mark) if mark >= 40: dst.write(f"{name},{city},{mark}\n") total += mark count += 1
Same conceptual algorithm; Pandas provides higher-level primitives.
91. Why learn manual processing first?
Without the earlier weeks:
pythondf[df["mark"] >= 40]
may look like magic.
But you can interpret it as:
textfor each row: check mark >= 40 retain matching row
The abstraction becomes understandable because you know the lower-level algorithm.
92. Common NumPy mistake: list semantics assumed
pythonnp.array([1,2,3]) * 2
does not duplicate the sequence.
It multiplies each numerical element.
Always track the datatype:
textlist vs NumPy array
93. Common NumPy mistake: incompatible shapes
Element-wise operations require compatible shapes.
For example, arrays containing unrelated dimensions may not combine the way you expect.
Shape is part of the data's structure.
Always inspect:
pythona.shape
when array operations behave unexpectedly.
94. Common NumPy mistake: and with arrays
Element-wise conditions typically use:
python& |
with parentheses:
python(a > 0) & (a < 10)
rather than scalar:
pythonand or
95. Common NumPy mistake: missing parentheses
Wrong:
pythona > 0 & a < 10
Operator precedence can produce unintended interpretation.
Safer:
python(a > 0) & (a < 10)
96. Common Pandas mistake: one vs many columns
One column:
pythondf["mark"]
usually produces a Series.
Multiple:
pythondf["name", "mark"](/viewer?path="name", "mark")
produces a DataFrame.
Single vs double brackets matter.
97. Common Pandas mistake: filtering without parentheses
For multiple conditions, prefer:
pythondf[(condition1) & (condition2)]
Parentheses around each comparison are important.
98. Common Pandas mistake: and/or in vector conditions
Like NumPy arrays, Pandas Series conditions commonly use:
python& |
for element-wise logical combination.
Not ordinary scalar
and / or.99. Common Pandas mistake: modifying unrelated copy/view assumptions
DataFrame selections can have subtleties around whether operations affect original data.
At introductory level, prefer explicit assignments and transformations rather than relying on implicit mutation behavior.
100. Common Pandas mistake: assuming missing means zero
A missing value:
textNaN
is not automatically:
Missing and zero are conceptually different.
101. Common Pandas mistake: ignoring datatype
A column containing:
text"10" "20" "30"
may be textual.
It is not necessarily the same as numerical:
text10 20 30
Inspect/convert datatypes when needed.
102. Common Pandas mistake: row index treated as data
The DataFrame index is metadata for row identification unless intentionally used as a data field.
Don't automatically interpret:
text0,1,2,...
as a meaningful dataset variable.
103. Trace NumPy example
pythona = np.array([2, 5, 8, 1]) mask = a > 4 b = a[mask] c = b * 2
Step 1:
texta = [2,5,8,1]
Step 2:
textmask = [False, True, True, False]
Step 3:
textb = [5,8]
Step 4:
textc = [10,16]
104. Trace Pandas example
Initial:
| name | mark |
|---|---|
| A | 40 |
| B | 90 |
| C | 75 |
Condition:
pythondf["mark"] >= 75
produces conceptually:
textFalse True True
Filtering gives:
| name | mark |
|---|---|
| B | 90 |
| C | 75 |
105. Transformation example
pythondf["double"] = df["mark"] * 2
Result:
| name | mark | double |
|---|---|---|
| A | 40 | 80 |
| B | 90 | 180 |
| C | 75 | 150 |
No explicit row loop is required.
106. Aggregation example
pythondf["mark"].max()
returns:
If you need the whole row associated with the maximum, the problem is different: preserve the row relationship rather than only calculating the scalar maximum.
107. High-level pattern equivalence
Manual filtering
pythonresult = [] for x in values: if x > 10: result.append(x)
NumPy
pythona[a > 10]
Pandas
pythondf[df["column"] > 10]
Same computational idea, different abstraction layers.
108. High-level aggregation equivalence
Python
pythonsum(values) / len(values)
NumPy
pythonnp.mean(a)
Pandas
pythondf["column"].mean()
The mathematical concept is unchanged.
109. Full course progression
The Python course now forms a fairly coherent ladder:
textWeek 1 Algorithms ↓ Weeks 2–3 Conditionals ↓ Weeks 4–5 Iteration ↓ Weeks 6–8 Collections ↓ Weeks 9–10 Files ↓ Week 11 Modules ↓ Week 12 NumPy + Pandas
Each layer builds on the previous one.
110. Computational hierarchy
You can now think about Python at several levels:
textVALUE 5 ↓ VARIABLE x = 5 ↓ CONTROL FLOW if / for / while ↓ COLLECTION list / dict / set ↓ PERSISTENCE files ↓ CODE ORGANIZATION modules ↓ DATA ABSTRACTION NumPy / Pandas
111. Compact NumPy cheat sheet
pythonimport numpy as np a = np.array([1, 2, 3]) a.shape a.ndim a.size a[0] a[1:3] a + 10 a * 2 a ** 2 a > 1 a[a > 1] np.sum(a) np.mean(a) np.min(a) np.max(a) np.arange(10) np.zeros(5) np.ones(5)
For 2D:
pythona[row, column] a[:, 0] a[0, :] a.reshape(...)
112. Compact Pandas cheat sheet
pythonimport pandas as pd df = pd.read_csv("data.csv") df.head() df.tail() df.shape df.columns df["mark"] df["name", "mark"](/viewer?path="name", "mark") df[df["mark"] >= 40] df["mark"].mean() df["mark"].sum() df["mark"].min() df["mark"].max() df["city"].value_counts() df.sort_values("mark") df.groupby("city")["mark"].mean() df.to_csv("output.csv", index=False)
113. Which abstraction should I use?
Simple handful of values
pythonlist
Key-value associations
pythondict
Unique membership
pythonset
Numerical arrays/matrices
pythonNumPy
Named columns / tables
pythonPandas
Persistent storage
textfiles
The data structure should match the problem.
114. What you should actually master
By the end of Python Week 12, you should be comfortable with:
- Explaining why NumPy is useful for numerical arrays.
- Importing NumPy as
np. - Creating NumPy arrays.
- Indexing and slicing arrays.
- Understanding array shape/dimensions.
- Performing element-wise arithmetic.
- Understanding vectorized computation.
- Aggregating with sum/mean/min/max.
- Performing Boolean comparisons on arrays.
- Filtering arrays with Boolean masks.
- Understanding basic multidimensional indexing.
- Explaining what a Pandas DataFrame represents.
- Importing Pandas as
pd. - Creating or reading tabular data.
- Selecting DataFrame columns.
- Filtering rows using Boolean conditions.
- Computing column aggregates.
- Sorting tabular data.
- Adding/transformation columns.
- Understanding Series vs DataFrame at a basic level.
- Understanding DataFrame indices.
- Recognizing missing values.
- Understanding the purpose of
groupby(). - Reading and writing CSV files using Pandas.
- Connecting NumPy/Pandas operations to the manual algorithms from earlier weeks.
If these are comfortable, Programming in Python Week 12 is under control.
Quick Recall & Flashcards
Test your active recall before proceeding to practice problems. Format:
Question :: Answer.- Week 12 Diagnostic: If
A = np.array([1, 2, 3]), what is the result ofA * 2? :: Answer: B — NumPy arrays perform element-wise vectorized arithmetic, soA * 2multiplies each element:array([2, 4, 6]). - Week 12 Diagnostic: In Pandas, which method returns the first 5 rows of a DataFrame
df? :: Answer: B —df.head()displays the top 5 rows of a Pandas DataFrame by default.
Practice & Self-Check
Attempt these diagnostic questions to verify your operational understanding.
1
Question 1
If
A = np.array([1, 2, 3]), what is the result of A * 2?MCQ
3 Unit Assessment
A. [1, 2, 3, 1, 2, 3]
B. array([2, 4, 6])
C. TypeError
D. array([1, 4, 9])
2
Question 2
In Pandas, which method returns the first 5 rows of a DataFrame
df?MCQ
3 Unit Assessment
A. df.start(5)
B. df.head()
C. df.top(5)
D. df.first(5)