Python Practice › NumPy Programs › Program 88

Array to a DataFrame with headers

EasyNumPy Programs · 88 of 100

Write a NumPy program to convert an array into a pandas DataFrame with column headers.

Advertisement
Output
   maths  science
0     90       85
1     72       64
2     88       91

Approach

  1. pd.DataFrame(array, columns=[...]).

Solution

Python
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.

More numpy programs

← All 100 NumPy Programs

Leave a Comment