Sunday 3 September 2023

Convert Pandas Series to NumPy Array in Python

 sing series ‘values’ attribute, we can get the array of values contained in the Series. The return type of values attribute is the NumPy array

 

get_series_values.py

import pandas as pd

primes = [2, 3, 5]
primes_index = ['first_prime', 'second_prime', 'third_prime']
primes_series = pd.Series(primes, index=primes_index, name='Hold the Prime numbers')

primes_series_values = primes_series.values

print('primes_series_values : ', primes_series_values)
print('type of primes_series_values : ', type(primes_series_values))

 

Output

primes_series_values :  [2 3 5]
type of primes_series_values :  <class 'numpy.ndarray'>

 

 

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment