Open In App

Python | Pandas Series.from_array()

Improve
Improve
Like Article
Like
Save
Share
Report

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.




# importing pandas as pd
import pandas as pd
  
# Creating an array
array = ['New York', 'Chicago', 'Toronto', 'Lisbon', 'Rio']
  
# Create the Index
index_ = ['City 1', 'City 2', 'City 3', 'City 4', 'City 5'
  
# Creating the series
sr = pd.Series.from_array(arr = array, index = index_)
  
# Print the 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.
 
Example #2: Use Series.from_array() function to construct a series object from an array. Also set the name of the series




# importing pandas as pd
import pandas as pd
  
# Creating an array
array = [11, 21, 8, 18, 65, 84, 32, 10, 5, 24, 32]
  
# Create the Index
index_ = pd.date_range('2010-10-09', periods = 11, freq ='M')
  
# Creating the series
# set the index
# set the name
sr = pd.Series.from_array(arr = array, index = index_, name = 'My_series')
  
# Print the 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
Previous
Next
Share your thoughts in the comments
Similar Reads