﻿ Pandas: Create a DataFrame from a Numpy array and specify the index column and column headers - w3resource # Pandas: Create a DataFrame from a Numpy array and specify the index column and column headers

## Pandas: DataFrame Exercise-44 with Solution

Write a Pandas program to create a DataFrame from a Numpy array and specify the index column and column headers.

Sample Solution :

Python Code :

``````import pandas
import numpy
dtype = [('Column1','int32'), ('Column2','float32'), ('Column3','float32')]
values = numpy.zeros(15, dtype=dtype)
index = ['Index'+str(i) for i in range(1, len(values)+1)]
df = pandas.DataFrame(values, index=index)
print(df)
``````

Sample Output:

```          Column1  Column2  Column3
Index1         0      0.0      0.0
Index2         0      0.0      0.0
Index3         0      0.0      0.0
Index4         0      0.0      0.0
Index5         0      0.0      0.0
Index6         0      0.0      0.0
Index7         0      0.0      0.0
Index8         0      0.0      0.0
Index9         0      0.0      0.0
Index10        0      0.0      0.0
Index11        0      0.0      0.0
Index12        0      0.0      0.0
Index13        0      0.0      0.0
Index14        0      0.0      0.0
Index15        0      0.0      0.0
```

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz

﻿

## Python: Tips of the Day

Dictionary comprehension:

```>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
```