Python Practice › NumPy Programs › Program 88
Array to a DataFrame with headers
Write a NumPy program to convert an array into a pandas DataFrame with column headers.
Output
maths science 0 90 85 1 72 64 2 88 91
Approach
pd.DataFrame(array, columns=[...]).
Solution
import numpy as npimport pandas as pd a = np.array([[90, 85], [72, 64], [88, 91]])df = pd.DataFrame(a, columns=["maths", "science"])print(df)Output
maths science 0 90 85 1 72 64 2 88 91
Learn the concept
New to this topic? Read the lesson, then come back.