How to group by multiple columns?

To group a DataFrame by multiple columns in Python using pandas, you can use the groupby() method and pass a list of column names. Here’s an example:

import pandas as pd

# Sample DataFrame
data = {
    'Course': ['Math', 'Science', 'Math', 'Science', 'Math'],
    'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
    'Score': [85, 90, 95, 80, 88]
}

df = pd.DataFrame(data)

# Group by 'Course' and 'Name'
grp = df.groupby(['Course', 'Name'])

# To see the grouped data, you can use an aggregation function
result = grp.mean()  # Example of calculating the mean score
print(result)

In this example, the DataFrame is grouped by the 'Course' and 'Name' columns, and the mean score is calculated for each group. You can replace mean() with other aggregation functions as needed.

0 Comments

no data
Be the first to share your comment!