Creating a Pandas DataFrame from a Numpy array: How do I specify the index column and column headers?
To create a Pandas DataFrame from a Numpy array and specify the index column and column headers, you can use the pd.DataFrame()
constructor and pass in the Numpy array, as well as the index
, columns
parameters.
Here is an example:
import numpy as np
import pandas as pd
# Numpy array
data = np.array([[1, 2, 3], [4, 5, 6]])
# Index column
index = ['row1', 'row2']
# Column headers
columns = ['col1', 'col2', 'col3']
# Create DataFrame
df = pd.DataFrame(data, index=index, columns=columns)
You will get a DataFrame like this:
col1 col2 col3 row1 1 2 3 row2 4 5 6