How to reshape Pandas Series?


We can reshape the Pandas series using ways like Transpose, reshape method, and melt function. A Pandas Series is a one-dimensional labeled array that can hold data of any type (integer, float, string, etc.). It is similar to a NumPy array but has an index associated with each element that can be used to access individual values. Reshaping refers to changing the shape or structure of the Pandas series for using the data in various ways.

Algorithm

The general algorithm to Reshape Pandas Series using different methods is as follows −

  • Create a Pandas Series with some data.

  • Use the reshape() method to change the shape of the Series to the desired shape.

  • Use the stack() method to pivot the Series from a wide format to a long format, if needed.

  • Use the melt() method to unpivot the Series from a wide format to a long format, if needed.

  • Use the unstack() method to pivot the Series from a long format to a wide format, if needed.

  • Use the pivot() method to pivot the Series from a long format to a wide format, if needed.

  • Use the T attribute to transpose the Series, if needed.

Method 1: Using the Transpose attribute

The transpose function (T) can be used to switch the rows and columns of a Series. This is useful when we want to visualize the data in a different way.

Syntax

Here, T is an attribute and not a method, so you don't need to use parentheses when using it. Also, because it's an attribute and not a method, it can't take any arguments. The T attribute returns a new DataFrame with the rows and columns interchanged.

Example

In the below example, we have created a pandas series and then used the transpose function to transpose the pandas series and finally printed the transposed series as output.

import pandas as pd

# Create a Series
s = pd.Series([1, 2, 3, 4])

# Transpose the Series
s_transposed = s.T

# Print the transposed Series
print(s_transposed)

Output

0    1
1    2
2    3
3    4
dtype: int64

Method 2: Using reshape method.

The reshape method can be used to change the shape of a Series. This method requires the new shape to be compatible with the original shape.

Syntax

DataFrame.reshape(shape[, order])

Here, the shape argument specifies the new dimensions of the array, while the optional order argument specifies the order in which the elements of the array are arranged.

Example

In the below example, the pandas series is reshaped using values.reshape() method.Firstly a series is created which contains values from 1 to 9. Then the values.reshape(3,3) is used to reshape the series to a matrix of size 3x3.

import pandas as pd
import numpy as np

# Create a Series
s = pd.Series(np.arange(1, 10))

# Reshape the Series
s_reshaped = s.values.reshape((3, 3))

# Print the reshaped Series
print(s_reshaped)

Output

[[1 2 3]
 [4 5 6]
 [7 8 9]]

Method 3: Using the Melt function

The melt function can be used to unpivot a Series. This function creates a new data frame with a column for each unique value in the original Series and a row for each unique combination of values.

Syntax

DataFrame.melt([id_vars, value_vars, ...], ...)

Here, the id_vars argument specifies the columns to use as identifier variables, the value_vars argument specifies the columns to unpivot, and additional arguments can be used to customize the output.

Example

In the below example, we first convert the Series s to a DataFrame using the reset_index method, which creates a new column 'index' containing the original Series index values. We then use the melt function on this DataFrame, specifying 'index' as the id_vars parameter and '0' (the name of the column containing the original Series values) as the value_vars parameter.

import pandas as pd

# Create a Series
s = pd.Series({'A': 1, 'B': 2, 'C': 3})

# Convert the Series to a DataFrame using reset_index()
df = s.reset_index()

# Melt the DataFrame
df_melted = pd.melt(df, id_vars='index', value_vars='0')

# Print the melted DataFrame
print(df_melted)

Output

  index variable  value
0     A        0      1
1     B        0      2
2     C        0      3

Method 4: Using the unstack() method

The unstack() method in Pandas is used to reshape a multi-level index Series or DataFrame into a wide format. The method essentially pivots the innermost level of a multi-level index to become the columns of a new DataFrame. The unstack() method is the inverse of the stack() method.

Syntax

Series.unstack(level=-1, fill_value=None)

Here, the level argument specifies the level(s) of the index to unstack, while the fill_value argument specifies the value to fill missing values with.

Example

In the below example, we first create a multi-level index DataFrame df with two levels of index, 'First' and 'Second'. We then use the

unstack() method on the DataFrame to pivot the innermost level 'Second' to become the columns of a new DataFrame. The resulting DataFrame df_unstacked has the columns 'A' and 'B', and the original levels of index 'First' and 'Second' are preserved as row labels.

import pandas as pd

# Create a multi-level index DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}, index=pd.MultiIndex.from_tuples([('X', 'a'), ('X', 'b'), ('Y', 'c')], names=['First', 'Second']))

# Unstack the DataFrame
df_unstacked = df.unstack()

# Print the unstacked DataFrame
print(df_unstacked)

Output

          A              B          
Second    a    b    c    a    b    c
First                               
X       1.0  2.0  NaN  4.0  5.0  NaN
Y       NaN  NaN  3.0  NaN  NaN  6.0

Method 5: Using the pivot() method

The pivot() method in Pandas is used to reshape a DataFrame from long to wide format. The method takes three parameters: index, columns, and values. The index parameter specifies the column(s) to use as the row index of the resulting DataFrame, the columns parameter specifies the column(s) to use as the column index of the resulting DataFrame, and the values parameter specifies the column(s) to use as the values of the resulting DataFrame.

Syntax

DataFrame.pivot([index, columns, values])

Here, the index argument specifies the column(s) to use as the row index, the columns argument specifies the column(s) to use as the column index, and the values argument specifies the column(s) to use as the data values.

Example

In the below example, we first create a long format DataFrame df with three columns: 'Year', 'Quarter', and 'Sales'. We then use the pivot() method on the DataFrame, specifying 'Year' as the index parameter, 'Quarter' as the columns parameter, and 'Sales' as the values parameter. The resulting DataFrame df_pivoted has two columns 'Q1' and 'Q2', with 'Year' as the row index.

import pandas as pd

# Create a long format DataFrame
df = pd.DataFrame({'Year': [2019, 2019, 2020, 2020], 'Quarter': ['Q1', 'Q2', 'Q1', 'Q2'], 'Sales': [100, 200, 150, 250]})

# Pivot the DataFrame
df_pivoted = df.pivot(index='Year', columns='Quarter', values='Sales')

# Print the pivoted DataFrame
print(df_pivoted)

Output

Quarter   Q1   Q2
Year             
2019     100  200
2020     150  250

Conclusion

In this article, we have discussed how we can reshape the Pandas series using methods like transpose, reshape and melt functions. We can reshape the pandas series to transform the data into a different format for visualization, Aggregation, or grouping of data and Merging and combining multiple data series into a data frame.

Updated on: 11-Jul-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements