In this python numpy program, we will convert a NumPy array to a data frame with headers.
Steps to solve the program
- Import the numpy library as np.
- Import pandas library as pd.
- Create an array using np.array().
- Convert an array to a data-frame with headers using pd.DataFrame() and set columns=[‘A’,’B’,’C’].
- Print the output.
import numpy as np
import pandas as pd
x = np.random.rand(6,3)
df = pd.DataFrame(x,columns=['A','B','C'])
print("\nPandas DataFrame: ")
print(df)
Output :
Pandas DataFrame:
A B C
0 0.307619 0.526206 0.982625
1 0.865438 0.248926 0.248677
2 0.080811 0.619051 0.774105
3 0.716698 0.419266 0.181618
4 0.223379 0.055573 0.168672
5 0.106160 0.929609 0.886170
create an array and reshape it.