Convert NumPy Array to pandas DataFrame in Python (2 Examples) | Create from Matrix | Columns & Rows

Published: 12 September 2022
on channel: Statistics Globe
2,297
36

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


On this page of the site you can watch the video online Convert NumPy Array to pandas DataFrame in Python (2 Examples) | Create from Matrix | Columns & Rows with a duration of hours minute second in good quality, which was uploaded by the user Statistics Globe 12 September 2022, share the link with friends and acquaintances, this video has already been watched 2,297 times on youtube and it was liked by 36 viewers. Enjoy your viewing!