Python | Pandas Series.from_array()
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index.
Pandas Series.from_array()
function construct a Series object from an array. We can also set the name and index of the series at the time of creation.
Syntax: Series.from_array(arr, index=None, name=None, dtype=None, copy=False, fastpath=False)
Parameter :
arr : array to be used for construction of series
index : set the index of the series
name : Set the name of the series
Returns : series
Example #1: Use Series.from_array()
function to construct a series object from an array.
import pandas as pd
array = [ 'New York' , 'Chicago' , 'Toronto' , 'Lisbon' , 'Rio' ]
index_ = [ 'City 1' , 'City 2' , 'City 3' , 'City 4' , 'City 5' ]
sr = pd.Series.from_array(arr = array, index = index_)
print (sr)
|
Output :
As we can see in the output, the Series.from_array()
function has successfully constructed a series object from an array.
Example #2: Use Series.from_array()
function to construct a series object from an array. Also set the name of the series
import pandas as pd
array = [ 11 , 21 , 8 , 18 , 65 , 84 , 32 , 10 , 5 , 24 , 32 ]
index_ = pd.date_range( '2010-10-09' , periods = 11 , freq = 'M' )
sr = pd.Series.from_array(arr = array, index = index_, name = 'My_series' )
print (sr)
|
Output :
As we can see in the output, the Series.from_array()
function has successfully constructed a series object from an array. We have also set the name of the series.
Last Updated :
13 Feb, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...