You can remove your for loop and do this instead:

Copydata = df.groupby(['ta', 'tb', 'tc'])['income'].sum()

Then plot it.

Answer from Mayank Porwal on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › pandas groupby error in function only?
r/learnpython on Reddit: Pandas Groupby Error in Function Only?
March 11, 2020 -

I get the error " AttributeError: 'str' object has no attribute 'groupby' " when I run the following piece of code:

def groupby(df):

median_df = df.groupby('Brand').median()

return median_df

median_customer_df = groupby('customer_df')

However when outside of the function I simply run:

median_df = customer_df.groupby('Brand').median()

the code runs fine?

Can anyone please help explain why this groupby is failing only when in my function?

Thanks in advance!

Discussions

python - don't know why: AttributeError: 'list' object has no attribute 'groupby' - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
python - AttributeError: 'DataFrame' object has no attribute 'group_by' - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
AttributeError: SeriesGroupBy object has no attribute ffill
Hi, I’m trying to convert some pandas code into Dask. In pandas, I can easily ffill my SeriesGroupBy object, toy example: import numpy as np import pandas as pd df = pd.DataFrame({ 'a_cat': list('aabbbaccc'), 'b_num': [np.nan if i%3!=0 else (i+1) for i in range(9)] }) df.groupby('a_cat')... More on dask.discourse.group
🌐 dask.discourse.group
3
0
February 10, 2022
python 3.x - Pandas groupby => AttributeError: 'function' object has no attribute 'mean' - Stack Overflow
5 groupby - TypeError 'DataFrame' object is not callable More on stackoverflow.com
🌐 stackoverflow.com
January 12, 2021
🌐
GitHub
github.com › pandas-dev › pandas › issues › 11640
BUG AttributeError: 'DataFrameGroupBy' object has no attribute '_obj_with_exclusions' · Issue #11640 · pandas-dev/pandas
November 18, 2015 - In [5]: df.groupby('a').mean() --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-29-a830c6135818> in <module>() ----> 1 df.groupby('a').mean() /home/nicolas/Git/pandas/pandas/core/groupby.py in mean(self) 764 self._set_selection_from_grouper() 765 f = lambda x: x.mean(axis=self.axis) --> 766 return self._python_agg_general(f) 767 768 def median(self): /home/nicolas/Git/pandas/pandas/core/groupby.py in _python_agg_general(self, func, *args, **kwargs) 1245 output[name] = self._try_cast(values[mask], result)
Author   nbonnotte
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 1.1 › reference › groupby.html
GroupBy — pandas 1.1.5 documentation
GroupBy objects are returned by groupby calls: pandas.DataFrame.groupby(), pandas.Series.groupby(), etc · Dict {group name -> group labels}
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › pandas attributeerror: 'dataframe' object has no attribute 'group_by'
r/learnpython on Reddit: Pandas AttributeError: 'DataFrame' object has no attribute 'group_by'
February 28, 2018 -

Hello,

Has anyone ever come across this before?

I'm trying to group some data in a dataframe and getting this error. The steps I've taken are:

  1. in a for loop:

read in a csv from an api using pd.read_csv() replaced some values in a column using a for loop and .loc[] appended the resulting data frame to a list

2) concatenated the list of dataframes using pd.concat()

3) added a calculated column to the new DF by multiplying another column

4) added two empty columns

5) filtered the DF using .loc[] based on a value within a column

6) filtered the DF using .loc[] based on a value in a different column

7) tried to use this code:

new_DF = old_df.group_by(['col1', 'col_2', 'col_3', 'adgroup', 'col_4', 'col5', 'col6'], as_index=False)[['col7', 'col8', 
'col9']].sum()

The DF seems to behaving normally for example I can do dtypes and columns on it and add columns which are calculated from other columns. What is super frustrating is that I can do pd.to_csv() and then pd.read_csv() on the DF and then I'm able to do the grouping I want (however this isn't ideal which is why I'm posting).

Any advice would be appreciated.

Cheers

🌐
Databricks Community
community.databricks.com › t5 › data-engineering › issue-with-pyspark-groupby-groupeddata › td-p › 7255
Issue with Pyspark GroupBy GroupedData - Databricks Community - 7255
March 27, 2023 - The issue with your code is that the groupBy operation returns a GroupedData object, which does not have a get_group method. Instead, you can use the filter method to filter the bronze_df DataFrame for each entity name and write the resulting DataFrames to separate Silver tables.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.groupby.html
pandas.Series.groupby — pandas 3.0.2 documentation
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). If a list or ndarray of length equal to the selected axis is passed (see the groupby user guide), the values are used as-is to determine the groups. A label or list of labels may be passed to group by the columns in self. Notice that a tuple is interpreted as a (single) key.
🌐
GitHub
github.com › dask › dask › issues › 3438
Dataframe groupby()[column].agg fails with AttributeError · Issue #3438 · dask/dask
April 24, 2018 - AttributeError Traceback (most recent call last) <ipython-input-1-b43466d1ae6a> in <module>() 6 -0.666]}) 7 ddf = dd.from_pandas(df, npartitions=1) ----> 8 ddf.groupby('A')['B'].agg('var') 9 10 ~/applications/anaconda3/lib/python3.6/site-packages/dask/dataframe/groupby.py in agg(self, arg, split_every, split_out) 1217 @derived_from(pd.core.groupby.SeriesGroupBy) 1218 def agg(self, arg, split_every=None, split_out=1): -> 1219 return self.aggregate(arg, split_every=split_every, split_out=split_out) ~/applications/anaconda3/lib/python3.6/site-packages/dask/dataframe/groupby.py in aggregate(self, arg, split_every, split_out) 1211 1212 if not isinstance(arg, (list, dict)): -> 1213 result = result[result.columns[0]] 1214 1215 return result AttributeError: 'Series' object has no attribute 'columns' No one assigned ·
Author   joergdietrich
🌐
Dataquest Community
community.dataquest.io › q&a › dq courses
Using groupby function and agg function, also, sorting pivot tables using a dictionary - DQ Courses - Dataquest Community
October 31, 2022 - Screen Link: [Learn data science with Python and R projects](https://I-94 project, with some additional exploration) There’s more detail in the version saved on my own computer, including a later attempt to do some stuff with a pivot table. Here it is. I-94 project plus some exploration.ipynb (210.7 KB) My Code: traffic_by_day=(day.groupby(['day']))['day','traffic_volume'].agg({'day','sum'}) What I expected to happen: I expected this to produce the sum of traffic volume for each...
🌐
Saturn Cloud
saturncloud.io › blog › solving-the-attributeerror-cannot-access-callable-attribute-groupby-of-dataframegroupby-objects-in-pandas
Solving the AttributeError: Cannot Access Callable Attribute 'groupby' of 'DataFrameGroupBy' Objects in Pandas | Saturn Cloud Blog
September 11, 2023 - This will not raise any error. The AttributeError: Cannot access callable attribute 'groupby' of 'DataFrameGroupBy' objects is a common error encountered while working with Pandas.
🌐
Data Science Dojo
discuss.datasciencedojo.com › python
How to get group from groupby by key? - Python - Data Science Dojo Discussions
February 24, 2023 - Hello, I am having trouble accessing a specific group from my pandas DataFrame using the groupby() method. I have grouped my DataFrame by the ‘A’ column, and now I am trying to access the group with the key ‘foo’. I have tried using the .get_group() method, but it returns the group ...
🌐
GitHub
github.com › pandas-dev › pandas › issues › 11562
'mode' not recognized by df.groupby().agg(), but pd.Series.mode works · Issue #11562 · pandas-dev/pandas
November 9, 2015 - df.groupby('B').agg('mode') ... AttributeError: Cannot access callable attribute 'mode' of 'DataFrameGroupBy' objects, try using the 'apply' method
Author   patricksurry
🌐
YouTube
youtube.com › watch
Resolving the AttributeError in Pandas: Understanding the DataFrameGroupBy Object - YouTube
Struggling with the `AttributeError` in Pandas when converting a `DataFrameGroupBy` object to a DataFrame? Discover the solution by applying aggregation firs...
Published   April 17, 2025
Views   0