Built with Tutify.io

    AIStudyLabs

    How to Reshape Pandas Series?

    How to Reshape Pandas Series?

    We can reshape the pandas series by using series.values.reshape() function. This reshape() function takes the dimension you wanted to reshape to. Note that this literally doesn't reshare the Series instead, it reshapes the output of Series.values which is a NumPy Ndarray.

    Before going to know the usage of reshape() we need to know about shape(), which is the number of elements in each dimension. Reshaping allows us to add or remove dimensions in an array. Reshape allows us to change the number of elements in each dimension.

    Key Points –

    <a id="h-1-quick-examples-of-reshap-pandas-series"></a>

    1. Quick Examples of Reshap Pandas Series

    If you are in a hurry, below are some quick examples of how to reshape the Series.

    
    # Below are the quick examples
    
    # Example 1:apply reshape() function to pandas series
    ser2 = ser.values.reshape((2,3))
      
    # Example 2: use reshape pandas series
    arr = ser.values
    ser2 = arr.reshape((2, 3))
    
    # Example 3: Get reshape series using array.reshape() function
    array = ['Spark','PySpark','Pandas','NumPy','Python',"Oracle"]
    ser = pd.Series(array)
    arr = ser.values
    ser2 = arr.reshape((3, 2))
    

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

    2. Syntax of Pandas Series reshape()

    Following is the syntax of the Pandas Series reshape() function.

    
    # Syntax of reshape()
    Pandas.Series.values.reshape((dimension))
    

    It returns an ndarray along with a specified shape.

    Now, let’s create pandas series using a list of values.

    
    import pandas as pd
    # Create a Series
    ser = pd.Series([2, 5, 8, 3, 6, 9])
    print(ser)
    

    <a id="h-4-apply-reshape-function-to-pandas-series"></a>

    4. Apply Reshape() Function to Pandas Series

    Use series.values.reshape() function we can change the dimensions of the array. Here I will apply reshape() function to pandas series it will return the specified shape array.

    
    # Apply reshape() function to pandas series
    ser2 = ser.values.reshape((2,3))
    print(ser2) 
      
    # Use reshape pandas series
    arr = ser.values
    ser2 = arr.reshape((2, 3))
    print(ser2)
    

    Yields below output.

    
    # Output:
    [[2 5 8]
     [3 6 9]]
    

    <a id="h-5-another-example-to-reshape-series"></a>

    5. Another Example of Reshape Series

    Here I will create pandas series using array, then apply reshape() function to given pandas series, it will return the specified shape of the array.

    
    # Get reshape series using array.reshape() function
    array = ['Spark','PySpark','Pandas','NumPy','Python',"Oracle"]
    ser = pd.Series(array)
    arr = ser.values
    ser2 = arr.reshape((3, 2))
    print(ser2)
    

    Yields below output.

    
    # Output:
    [['Spark' 'PySpark']
     ['Pandas' 'NumPy']
     ['Python' 'Oracle']]
    

    <a id="h-6-complete-example-for-reshap-pandas-series"></a>

    6. Complete Example For Reshap Pandas Series

    
    import pandas as pd
    
    # Create a Series
    ser = pd.Series([2, 5, 8, 3, 6, 9])
    print(ser)
    
    # Apply reshape() function to pandas series
    ser2 = ser.values.reshape((2,3))
    print(ser2) 
      
    # Use reshape pandas series
    arr = ser.values
    ser2 = arr.reshape((2, 3))
    print(ser2)
    
    # Get reshape series using array.reshape() function
    array = ['Spark','PySpark','Pandas','NumPy','Python',"Oracle"]
    ser = pd.Series(array)
    arr = ser.values
    ser2 = arr.reshape((3, 2))
    print(ser2)
    

    Frequently Asked Questions on Reshape Pandas Series

    Reshaping in Pandas Series refers to the process of altering the structure or arrangement of the data to facilitate analysis or to meet specific requirements.

    You may need to reshape a Series when you want to transform data from long to wide format or vice versa, rearrange the index or columns, or aggregate data based on certain criteria.

    Reshaping typically does not alter the underlying data; instead, it rearranges its presentation. However, improper use of reshaping methods or incorrect interpretation of the reshaped data could lead to erroneous conclusions. Always validate the results after reshaping to ensure data integrity.

    Reshaping operations in Pandas are reversible. You can often revert the data to its original shape using appropriate inverse reshaping methods or by keeping track of the transformation steps.

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

    Conclusion

    In this article, I have explained how to reshape the pandas series using series.values.reshape(), array.reshape() functions with examples.

    Happy Learning !!

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

    Related Articles

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

    References