Build DataFrames from 1-D and 2-D NumPy arrays, preserve numeric dtypes and convert selected columns back to arrays.
Show Table of Contents ↓import pandas as pd
import numpy as np
marks = np.array([30, 40, 50, 45])
single = pd.DataFrame(marks, columns=["MATH"])
print(single)
assert single.shape == (4, 1)Expected output
MATH
0 30
1 40
2 50
3 45subjects = np.array([[30, 40, 50, 45], [50, 60, 50, 55]])
raw = pd.DataFrame(subjects)
print(raw)
assert raw.shape == (2, 4)Expected output
0 1 2 3
0 30 40 50 45
1 50 60 50 55scores = pd.DataFrame(subjects).T
scores.columns = ["MATH", "ENGLISH"]
print(scores)
assert scores.shape == (4, 2)
assert scores.iloc[0].tolist() == [30, 50]Expected output
MATH ENGLISH
0 30 50
1 40 60
2 50 50
3 45 55names = np.array(["Alex", "Ron", "Jack", "King"])
legacy = pd.DataFrame([names, subjects[0], subjects[1]]).T
legacy.columns = ["NAMES", "MATH", "ENGLISH"]
print("Original mixed layout:")
print(legacy)
students = pd.DataFrame({"NAMES": names, "MATH": subjects[0], "ENGLISH": subjects[1]})
print("Numeric marks preserved:")
print(students)
assert pd.api.types.is_integer_dtype(students["MATH"])
assert pd.api.types.is_integer_dtype(students["ENGLISH"])Expected output
Original mixed layout:
NAMES MATH ENGLISH
0 Alex 30 50
1 Ron 40 60
2 Jack 50 50
3 King 45 55
Numeric marks preserved:
NAMES MATH ENGLISH
0 Alex 30 50
1 Ron 40 60
2 Jack 50 50
3 King 45 55math = np.array([30, 40, 50, 45])
english = np.array([50, 60, 50, 55])
result = pd.DataFrame({"NAMES": names, "MATH": math, "ENGLISH": english})
result["Total"] = result["MATH"] + result["ENGLISH"]
print(result)
assert result["Total"].tolist() == [80, 100, 100, 100]Expected output
NAMES MATH ENGLISH Total
0 Alex 30 50 80
1 Ron 40 60 100
2 Jack 50 50 100
3 King 45 55 100print(students.to_string(index=False))
assert students.index.tolist() == [0, 1, 2, 3]Expected output
NAMES MATH ENGLISH
Alex 30 50
Ron 40 60
Jack 50 50
King 45 55rng = np.random.default_rng(42)
n = 5
random_students = pd.DataFrame({"ID": np.arange(1, n + 1),
"MATH": rng.integers(40, 100, size=n), "ENG": rng.integers(40, 100, size=n)})
print(random_students[["MATH", "ENG"]])
print(random_students.to_string(index=False))
assert random_students["ID"].tolist() == [1, 2, 3, 4, 5]
assert random_students[["MATH", "ENG"]].ge(40).all().all()
assert random_students[["MATH", "ENG"]].lt(100).all().all()Expected output
MATH ENG
0 45 91
1 86 45
2 79 81
3 66 52
4 65 45
ID MATH ENG
1 45 91
2 86 45
3 79 81
4 66 52
5 65 45features = students[["MATH", "ENGLISH"]].to_numpy(dtype="float64", copy=True)
print(features)
features[0, 0] = -1
assert students.loc[0, "MATH"] == 30
assert features.shape == (4, 2)Expected output
[[30. 50.]
[40. 60.]
[50. 50.]
[45. 55.]]nullable = pd.DataFrame({"MATH": pd.Series([30, None, 50], dtype="Int64")})
exported = nullable.to_numpy(dtype="float64", na_value=np.nan)
print(exported)
assert np.isnan(exported[1, 0])Expected output
[[30.]
[nan]
[50.]]matrix = students[["MATH", "ENGLISH"]].to_numpy(copy=True)
answer = students.copy()
answer["Total"] = matrix.sum(axis=1)
print(answer)
assert answer["Total"].tolist() == [80, 100, 100, 100]
assert "Total" not in students.columnsExpected output
NAMES MATH ENGLISH Total
0 Alex 30 50 80
1 Ron 40 60 100
2 Jack 50 50 100
3 King 45 55 100Open in Google Colab View on GitHub
Run examples in order, inspect formatted dates and try the exercise. Save a copy to keep edits.
Reference: Pandas NumPy arrays documentation.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.