Convert a NumPy array to a Pandas series
Let us see how to convert a NumPy array to a Pandas series. A NumPy array can be converted into a Pandas series by passing it in the pandas.Series() function.
Example 1 :
# importing the modulesimport numpy as npimport pandas as pd # creating an NumPy arrayarray = np.array([10, 20, 1, 2, 3, 4, 5, 6, 7]) # displaying the NumPy arrayprint("Numpy array is :")display(array) # converting the NumPy array # to a Pandas seriesseries = pd.Series(array) # displaying the Pandas seriesprint("Pandas Series : ")display(series) |
Output :
Example 2 :
# importing the modulesimport numpy as npimport pandas as pd # creating an NumPy arrayarray = np.random.rand(5) # displaying the NumPy arrayprint("Numpy array is :")display(array) # converting the NumPy array # to a Pandas seriesseries = pd.Series(array) # displaying the Pandas seriesprint("Pandas Series : ")display(series) |
Output :






Please Login to comment...