Built with Tutify.io

    AIStudyLabs

    How to Convert NumPy Array to Pandas Series?

    How to Convert NumPy Array to Pandas Series?

    pandas.Series() function is used to convert the NumPy array to Pandas Series. Pandas Series and NumPy array have a similar feature in structure so, we can convert very easily from array to the series. To convert the array to Series we need to import both the NumPy module and Pandas module.

    In this article, I will explain pandas.series() function using this how we can convert From NumPy array to Pandas Series. In case, if you want to convert Pandas Series to NumPy array use Series.to_numpy().

    Key Points –

    <a id="h-1-quick-examples-of-convert-array-to-pandas-series"></a>

    1. Quick Examples of Convert Array to Pandas Series

    If you are in hurry below are some quick examples of the NumPy Array to Pandas Series conversion.

    
    # Quick examples of convert array to pandas series
    
    # Example 1: Create a NumPy array 
    # Using np.zeros()
    array = np.zeros(5)
    
    # Convert the NumPy array
    # To a Pandas series
    series = pd.Series(array)
    
    # Example 2: Create a NumPy array numpy.ones()
    array = np.ones(5)
    
    # Convert the NumPy array
    # To a Pandas series
    series = pd.Series(array)
    
    # Example 3 : Create a NumPy array 
    # Using random.rand()
    array = np.random.rand(5)
    
    # Convert the NumPy array
    # To a Pandas series
    series = pd.Series(array)
    

    <a id="h-2-syntax-of-pandas-series"></a>

    2. Syntax of pandas.Series()

    Following is the syntax of the

    
    # Syntax of pandas.Series()
    (data=None, index=None, dtype=None, name=None, copy=False, fastpath=False)
    

    <a id="h-2-1-parameters"></a>

    2.1 Parameters

    <a id="h-2-2-return-value"></a>

    2.2 Return Value

    It returns Pandas Series.

    <a id="h-3-convert-numpy-array-to-pandas-series"></a>

    3. Convert NumPy Array to Pandas Series

    NumPy array is a data structure (usually numbers), all of the same type, it looks the same as a list. But arrays are more efficient than Python lists and also much more compact. We can also convert Pandas DataFrame to a Numpy array.

    Pandas Series is a one-dimensional array that is capable of storing various data types (integer, string, float, python objects, etc.). We can easily convert the Pandas Series to list, Series to the dictionary, and Series to tuple using the Series() method. Let's create NumPy array using np.zeros() function then, convert array to Series using pd.series() function.

    
    import pandas as pd
    import numpy as np
    
    # Create a NumPy array using np.zeros()
    array = np.zeros(5)
    print("NumPy array:\n", array)
    
    # Convert the NumPy array to a Pandas series
    series = pd.Series(array)
    print(" Pandas Series:\n", series)
    print(type(series))
    

    Yields below output.

    
    # Output:
    NumPy array:
     [0. 0. 0. 0. 0.]
    
    Pandas Series:
    0    0.0
    1    0.0
    2    0.0
    3    0.0
    4    0.0
    dtype: float64
    

    <a id="h-3-1-use-ones-function-create-array-convert-series"></a>

    3.1 Use ones() Function Create Array & Convert Series.

    Here, we can create Numpy array using np.ones()function then, convert array to Series using pandas series() function. It will return the Pandas Series.

    
    # Create a NumPy array numpy.ones()
    array = np.ones(5)
    print("NumPy array:\n", array)
    
    # Convert the NumPy array to Pandas series
    series = pd.Series(array)
    print(" Pandas Series:\n", series)
    print(type(series))
    

    Yields below output.

    
    # Output:
    
    NumPy array:
     [1. 1. 1. 1. 1.]
    
    Pandas Series:
    0    1.0
    1    1.0
    2    1.0
    3    1.0
    4    1.0
    dtype: float64
    

    <a id="h-3-2-use-numpy-random-rand-function-create-array-convert-series"></a>

    3.2 Use numpy. random.rand() Function Create Array & Convert Series.

    Use numpy.random.rand() function create Numpy arraythen, convert array to Series using pd.series() function.

    
    # Create a NumPy array using random.rand()
    array = np.random.rand(5)
    print("NumPy array:\n", array)
    
    # Convert the NumPy array
    # To a Pandas series
    series = pd.Series(array)
    print(" Pandas Series:\n", series)
    print(type(series))
    

    Yields below output.

    
    # Output:
    
    NumPy array:
     [0.13894623 0.96695242 0.41112637 0.19469214 0.21145509]
    
    Pandas Series:
    0    0.639158
    1    0.272269
    2    0.402008
    3    0.462556
    4    0.490533
    dtype: float64
    

    Frequently Asked Questions on Convert NumPy Array to Series

    Converting a NumPy array to a Pandas Series allows you to leverage the extensive functionality and flexibility offered by Pandas. Series provide labeled indices, alignment, and a wide range of data manipulation capabilities not available in NumPy arrays.

    Converting a NumPy array to a Pandas Series does not change the underlying data. It merely provides a different interface for accessing and manipulating the data.

    Pandas Series can handle missing or NaN values, and they will be preserved during the conversion process. NaN values in NumPy arrays will be represented as NaN in the resulting Pandas Series.

    Pandas Series are one-dimensional data structures. However, you can convert a multi-dimensional NumPy array into a Pandas DataFrame, which is a two-dimensional data structure, using the pd.DataFrame() constructor.

    <a id="h-conclusion"></a>

    Conclusion

    In this article, I have explained pandas.series() function using this how we can convert from NumPy array to Pandas Series with multiple examples.

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References