How to create a pandas DataFrame from a NumPy array in the Python programming language. More details: https://statisticsglobe.com/convert-n...
Python code of this video:
import numpy as np # Load NumPy
my_array = np.array([[1, 2, 3, 4], # Create NumPy array
[11, 12, 13, 14],
[21, 22, 23, 24]])
print(my_array) # Print NumPy array
[[ 1 2 3 4]
[11 12 13 14]
[21 22 23 24]]
import pandas as pd # Import pandas library in Python
my_data1 = pd.DataFrame({'c1': my_array[:, 0], # Create pandas DataFrame
'c2': my_array[:, 1],
'c3': my_array[:, 2],
'c4': my_array[:, 3]})
print(my_data1) # Print pandas DataFrame
my_data2 = pd.DataFrame({'r1': my_array[0, :], # Create pandas DataFrame
'r2': my_array[1, :],
'r3': my_array[2, :]})
print(my_data2) # Print pandas DataFrame
Follow me on Social Media:
Facebook – Statistics Globe Page: / statisticsglobecom
Facebook – R Programming Group for Discussions & Questions: / statisticsglobe
Facebook – Python Programming Group for Discussions & Questions: / statisticsglobepython
LinkedIn – Statistics Globe Page: / statisticsglobe
LinkedIn – R Programming Group for Discussions & Questions: / 12555223
LinkedIn – Python Programming Group for Discussions & Questions: / 12673534
Twitter: / joachimschork
Music by bensound.com