Q:

Write a NumPy program to convert Pandas dataframe to Numpy array with headers

0

Write a NumPy program to convert Pandas dataframe to Numpy array with headers

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import numpy as np
import pandas as pd
np_array = np.random.rand(12,3)
print("Original Numpy array:")
print(np_array)
print("Type: ",type(np_array))
df = pd.DataFrame(np.random.rand(12,3),columns=['A','B','C'])
print("\nPanda's DataFrame: ")
print(df)
print("Type: ",type(df))

Sample Output:

Original Numpy array:
[[0.18308157 0.32258608 0.39644848]
 [0.31018507 0.08220454 0.40018982]
 [0.63639779 0.34908174 0.39878868]
 [0.74477532 0.4250794  0.12355822]
 [0.61842804 0.78525555 0.95617131]
 [0.17971402 0.03450462 0.3756935 ]
 [0.34533001 0.68281693 0.42670306]
 [0.90444404 0.96700828 0.87172799]
 [0.53261873 0.18419712 0.54780473]
 [0.50476661 0.82657057 0.06935023]
 [0.02482073 0.02318678 0.36032194]
 [0.69001834 0.04285817 0.31768865]]
Type:  <class 'numpy.ndarray'>

Panda's DataFrame: 
           A         B         C
0   0.015583  0.968431  0.943192
1   0.221607  0.704098  0.241200
2   0.466129  0.269226  0.450781
3   0.747222  0.674508  0.686070
4   0.908001  0.399580  0.124778
5   0.540362  0.024179  0.560710
6   0.572349  0.992342  0.347848
7   0.609707  0.786937  0.150088
8   0.162281  0.412158  0.761437
9   0.969180  0.215166  0.769721
10  0.272827  0.817238  0.093650
11  0.196416  0.643602  0.262683
Type:  <class 'pandas.core.frame.DataFrame'>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now