Use GroupBy.sum:
df.groupby(['Fruit','Name']).sum()
Out[31]:
Number
Fruit Name
Apples Bob 16
Mike 9
Steve 10
Grapes Bob 35
Tom 87
Tony 15
Oranges Bob 67
Mike 57
Tom 15
Tony 1
To specify the column to sum, use this: df.groupby(['Name', 'Fruit'])['Number'].sum()
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.groupby.html
pandas.DataFrame.groupby — pandas 3.0.6 documentation
Used to determine the groups for the groupby. If by is a function, it’s called on each value of the object’s index. If a dict or Series is passed, the Series or dict VALUES will be used to determine the groups (the Series’ values are first aligned; see .align() method).
Top answer 1 of 11
460
Use GroupBy.sum:
df.groupby(['Fruit','Name']).sum()
Out[31]:
Number
Fruit Name
Apples Bob 16
Mike 9
Steve 10
Grapes Bob 35
Tom 87
Tony 15
Oranges Bob 67
Mike 57
Tom 15
Tony 1
To specify the column to sum, use this: df.groupby(['Name', 'Fruit'])['Number'].sum()
2 of 11
266
Also you can use agg function,
df.groupby(['Name', 'Fruit'])['Number'].agg('sum')
python - How to use df.groupby() to select and sum specific columns w/o pandas trimming total number of columns - Data Science Stack Exchange
I got Column1, Column2, Column3, Column4, Column5, Column6 I'd like to group Column1 and get the row sum of Column3,4 and 5 When I apply groupby() and get this that is correct but it's leaving out More on datascience.stackexchange.com
How can I group and sum certain columns of a pandas dataframe
Yes. Bin the column names using pd.cut , which returns an object containing the bin label where each column name falls into. Use those labels to group your columns (axis=1 ) using groupby, and aggregate the results with sum. I'm assuming that your column names are floats and not strings. More on reddit.com
Pandas groupby.sum() or grouby.agg(sum) to retain variable as index?
I have a somewhat larger data set. I want to create a summary for a specific column, with the totals of two other column. So Column A contains a… More on reddit.com
Pandas group by column find percentage of count in each group
Surely you want to sum the total, not the survival indicator? train_df.groupby('Sex')['Total'].transform('sum') Then it's straightforward scalar arithmetic. More on reddit.com
04:51
Groupby Function in Python Pandas sum, min, max, count with Group ...
11:05
Group By and Aggregate Functions in Pandas | Python Pandas Tutorials ...
03:21
Calculate Sum by Group in Python (2 Examples) | DataFrame Subgroups ...
01:19
Solving the groupby.sum() Dilemma in Pandas DataFrames - YouTube
01:31
How to Sum a Column in a DataFrame Using Grouper in Pandas - YouTube
02:05
Mastering Pandas Groupby, Sum, and Flatten Your Dataframe - YouTube
GeeksforGeeks
geeksforgeeks.org › python › pandas-groupby-and-sum
Pandas Groupby and Sum - GeeksforGeeks
July 23, 2025 - Example 1: Pandas groupby() & sum() by Column Name · In this example, we group data on the Points column and calculate the sum for all numeric columns of DataFrame. Python3 · # use groupby() to compute sum df.groupby(['Points']).sum() Output: Example 2: Pandas groupby() & sum() on Multiple Columns ·
Pandas
pandas.pydata.org › docs › user_guide › groupby.html
Group by: split-apply-combine — pandas 3.0.6 documentation
SELECT Column1, Column2, mean(Column3), sum(Column4) FROM SomeTable GROUP BY Column1, Column2 · We aim to make operations like this natural and easy to express using pandas.
Codecademy
codecademy.com › docs › python:pandas › groupby › .sum()
Python:Pandas | GroupBy | .sum() | Codecademy
April 20, 2023 - The .sum() method produces a new Series or DataFrame with aggregate sums for the groups in a GroupBy object.
Pandas
pandas.pydata.org › pandas-docs › stable › search.html
Search - pandas 3.0.6 documentation
Created using Sphinx 9.0.4 · Built with the PyData Sphinx Theme 0.16.1
Facebook
facebook.com › groups › statisticsglobepython › posts › 736489724995518
How to calculate sum by group in pandas
Popular groups · Find communities for you · Over 1 billion people across the globe are using Facebook Groups to explore their favorite topics · Log in · Categories · Science & tech · Travel · Animals · Sports & fitness · Entertainment
Apache
spark.apache.org › docs › latest › api › python › reference › pyspark.pandas › api › pyspark.pandas.groupby.GroupBy.sum.html
pyspark.pandas.groupby.GroupBy.sum — PySpark 4.2.0 documentation
Skip to main content · GitHub · pyspark.pandas.groupby.GroupBy.sum# · GroupBy.sum(numeric_only=False, min_count=0)[source]# · Compute sum of group values · New in version 3.3.0 · Parameters · numeric_onlybool, default False · Include only float, int, boolean columns · New in version 3.4.0
Reddit
reddit.com › r/learnpython › how can i group and sum certain columns of a pandas dataframe
r/learnpython on Reddit: How can I group and sum certain columns of a pandas dataframe
May 25, 2021 -
I have a pandas dataframe with 6000 columns whose column names are decimal values (i.e. 0.002, 0.054, 1.03, ..., 250.045).
I am hoping to create a new dataframe with 251 columns from this whose columns are instead 0 to 1, 1 to 2, ..., 250 to 251.
Is there a way to do this in pandas (without looping)?
Top answer 1 of 2
3
Yes. Bin the column names using pd.cut , which returns an object containing the bin label where each column name falls into. Use those labels to group your columns (axis=1 ) using groupby, and aggregate the results with sum. I'm assuming that your column names are floats and not strings.
2 of 2
2
First, create a mapping between the 6000 unique column names and the 251 unique binned names: >>> bins = pd.cut(df.columns, bins=251, include_lowest=True) Second, rename the columns using the mapped bin values: >>> df = df.rename(columns=dict(zip(df.columns, bins))) Third, group on column name bins and sum within each group: >>> df = df.groupby(df.columns, axis=1).sum() You can avoid modifying your original dataframe by passing the operation from step 1 into df.groupby in place of df.columns: >>> df.groupby(pd.cut(df.columns, bins=251, include_lowest=True), axis=1).sum() But that's kinda hard to read IMHO. Up to you!
Reddit
reddit.com › r/learnpython › pandas groupby.sum() or grouby.agg(sum) to retain variable as index?
r/learnpython on Reddit: Pandas groupby.sum() or grouby.agg(sum) to retain variable as index?
November 10, 2021 - I have a somewhat larger data set. I want to create a summary for a specific column, with the totals of two other column. So Column A contains a…
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.sum.html
pandas.DataFrame.sum — pandas 3.0.6 documentation
Return the sum of the values over the requested axis.
Pandas
pandas.pydata.org › pandas-docs › version › 2.2 › reference › api › pandas.core.groupby.DataFrameGroupBy.sum.html
pandas.core.groupby.DataFrameGroupBy.sum — pandas 2.2.3 documentation
>>> data = [[1, 8, 2], [1, 2, 5], [2, 5, 8], [2, 6, 9]] >>> df = pd.DataFrame(data, columns=["a", "b", "c"], ... index=["tiger", "leopard", "cheetah", "lion"]) >>> df a b c tiger 1 8 2 leopard 1 2 5 cheetah 2 5 8 lion 2 6 9 >>> df.groupby("a").sum() b c a 1 10 7 2 11 17
Shane Lynn
shanelynn.ie › home › use pandas groupby to group and summarise dataframes
Group and Aggregate your Data Better using Pandas Groupby
October 16, 2021 - In older Pandas releases (< 0.20.1), renaming the newly calculated columns was possible through nested dictionaries, or by passing a list of functions for a column. Our final example calculates multiple values from the duration column and names the results appropriately. Note that the results have multi-indexed column headers. Note this syntax will no longer work for new installations of Python Pandas. # Define the aggregation calculations aggregations = { # work on the "duration" column 'duration': { # get the sum, and call this result 'total_duration' 'total_duration': 'sum', # get mean, cal
Medium
medium.com › @fintechashish › data-science-using-python-groupby-categories-in-a-column-for-the-sum-of-values-in-other-columns-96574489825
Data Science using Python : groupby categories in a column for the sum of values in other columns in a pandas dataframe(with conditions) | by Ashish Sharma | Medium
May 12, 2020 - Step : 4 — To analyse the total sales done by each employee, we need to use add all the columns on axis=1 of the data frame sales1. ... For those who doesn’t want to create a new dataframe they can simply use the code sales.groupby([‘Employee’])[‘Spring’,’Winter’,’Autumn’, ‘Summer’].sum().sum(axis=1) and they will get the same result.




