Creating a Pandas DataFrame from a Numpy array: How do I specify the index column and column headers?

I have a Numpy array consisting of a list of lists, representing a two-dimensional array with row labels and column names as shown below:

data = np.array([['','Col1','Col2'],['Row1',1,2],['Row2',3,4]])

I'd like the resulting DataFrame to have Row1 and Row2 as index values, and Col1, Col2 as header values.

I can specify the index as follows:

df = pd.DataFrame(data, index=data[:,0])

However, I am unsure how to best assign column headers.

← Назад к списку