The groupby()
function in Pandas is a powerful tool for performing complex data manipulations and aggregations. It allows you to split data into groups based on some criteria, apply a function to each group independently, and then combine the results back together.
In this example, we will group a DataFrame by a single column and calculate the sum of another column for each group.
import pandas as pd
data = {'Category': ['A', 'B', 'A', 'B'],
'Values': [10, 20, 30, 40]}
df = pd.DataFrame(data)
grouped = df.groupby('Category').sum()
print(grouped)
The DataFrame is grouped by the 'Category' column. The sum()
function is applied to aggregate the 'Values' column for each category.
Console Output:
Category A 40 B 60
This example demonstrates how to group a DataFrame by multiple columns and calculate the mean of another column for each group.
import pandas as pd
data = {'Category': ['A', 'A', 'B', 'B'],
'Subcategory': ['X', 'Y', 'X', 'Y'],
'Values': [10, 20, 30, 40]}
df = pd.DataFrame(data)
grouped = df.groupby(['Category', 'Subcategory']).mean()
print(grouped)
The DataFrame is grouped by both 'Category' and 'Subcategory'. The mean()
function is used to calculate the average 'Values' for each group.
Console Output:
Category Subcategory A X 10.0 Y 20.0 B X 30.0 Y 40.0
In this example, we will apply multiple aggregation functions to a grouped DataFrame.
import pandas as pd
data = {'Category': ['A', 'B', 'A', 'B'],
'Values': [10, 20, 30, 40]}
df = pd.DataFrame(data)
grouped = df.groupby('Category').agg(['sum', 'mean'])
print(grouped)
The DataFrame is grouped by the 'Category' column. Both sum()
and mean()
functions are applied to the 'Values' column for each group.
Console Output:
Values sum mean Category A 40 20.0 B 60 30.0
This example shows how to filter groups based on a condition after grouping.
import pandas as pd
data = {'Category': ['A', 'B', 'A', 'B'],
'Values': [10, 20, 30, 40]}
df = pd.DataFrame(data)
grouped = df.groupby('Category').filter(lambda x: x['Values'].sum() > 30)
print(grouped)
The DataFrame is grouped by 'Category', and only those groups where the sum of 'Values' exceeds 30 are retained.
Console Output:
Category Values 1 B 20 3 B 40
In this example, we will transform each group by applying a function to each element in the group.
import pandas as pd
data = {'Category': ['A', 'B', 'A', 'B'],
'Values': [10, 20, 30, 40]}
df = pd.DataFrame(data)
df['Transformed'] = df.groupby('Category')['Values'].transform(lambda x: x / x.sum())
print(df)
The DataFrame is grouped by 'Category', and the 'Values' column is transformed by dividing each value by the sum of its group.
Console Output:
Category Values Transformed 0 A 10 0.250000 1 B 20 0.333333 2 A 30 0.750000 3 B 40 0.666667
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies