How to Reshape a Series Dataframe in Python?

Estimated read time 2 min read

To reshape a Pandas DataFrame in Python, you can use the pivot or melt functions. These functions allow you to transform the structure of the DataFrame based on specific criteria. Here are examples of using both functions:

  1. Reshaping with pivot:
import pandas as pd

# Create a DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],
        'Subject': ['Math', 'Science', 'Math', 'Science'],
        'Score': [80, 90, 75, 85]}

df = pd.DataFrame(data)

# Reshape the DataFrame using pivot
reshaped_df = df.pivot(index='Name', columns='Subject', values='Score')

print(reshaped_df)

The output will be:

Subject  Math  Science
Name                  
Alice    80.0      NaN
Bob       NaN     90.0
Charlie  75.0      NaN
David     NaN     85.0

In this example, we have a DataFrame with three columns: “Name,” “Subject,” and “Score.” We use the pivot function and specify the index as “Name,” columns as “Subject,” and values as “Score.” This reshapes the DataFrame by setting the unique values in the “Name” column as the new index, the unique values in the “Subject” column as new columns, and populating the values with the corresponding “Score” values.

  1. Reshaping with melt:
import pandas as pd

# Create a DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],
        'Math': [80, 90, 75, 85],
        'Science': [85, 95, 70, 80]}

df = pd.DataFrame(data)

# Reshape the DataFrame using melt
reshaped_df = pd.melt(df, id_vars='Name', var_name='Subject', value_name='Score')

print(reshaped_df)

The output will be:

      Name  Subject  Score
0    Alice     Math     80
1      Bob     Math     90
2  Charlie     Math     75
3    David     Math     85
4    Alice  Science     85
5      Bob  Science     95
6  Charlie  Science     70
7    David  Science     80

In this example, we have a DataFrame with three columns: “Name,” “Math,” and “Science.” We use the melt function and specify the id_vars as “Name,” var_name as “Subject,” and value_name as “Score.” This reshapes the DataFrame by converting the columns into rows. The “Name” column remains as an identifier, and the “Math” and “Science” columns are combined into a single column called “Subject,” with corresponding values in the “Score” column.

These methods provide flexibility in reshaping DataFrames based on different column values and structures. Choose the method that suits your specific data and reshaping requirements.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply