Step 1: Define an array.
arr = np.array([
['Krishna', 35, 'Male'],
['Thulasi', 24, 'Female'],
['Raj', 41, 'Male']
])
Above snippet catures employees information.
Step 2: Convert Numpy array to the dataframe by assigning the array to the dataframe.
column_names = ['name', 'age', 'gender']
df = pd.DataFrame()
df[column_names] = arr
Find the below working application.
map_ndarray_to_df.py
import numpy as np
import pandas as pd
arr = np.array([
['Krishna', 35, 'Male'],
['Thulasi', 24, 'Female'],
['Raj', 41, 'Male']
])
print(f'arr : \n{arr}')
column_names = ['name', 'age', 'gender']
df = pd.DataFrame()
df[column_names] = arr
print(f'\ndf : \n{df}')
Output
arr : [['Krishna' '35' 'Male'] ['Thulasi' '24' 'Female'] ['Raj' '41' 'Male']] df : name age gender 0 Krishna 35 Male 1 Thulasi 24 Female 2 Raj 41 Male
No comments:
Post a Comment