Following the documentation for the pandas.DataFrame.groupby method, there are a couple ways you could fix this. The way I'd recommend is to explicitly specify the grouping column using the by parameter (although you don't need to), and then providing an aggregation function (looks like you want the mean). Any of these will work:

# Option 1
New.groupby(['P5PN']).mean()

# Option 2
New.groupby('P5PN').mean()

# Option 3
New.groupby(by=['P5PN']).mean()

The issue here is that while you are specifying a grouping column, you are not telling pandas how to aggregate the measures in the other columns of your data.

Note: you might want to update the title of your question to "'SeriesGroupBy' object has no attribute 'shape'" to match the actual error you're getting.

Answer from matsuninja on Stack Overflow
Discussions

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
python - AttributeError: 'DataFrame' object has no attribute 'group' - 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 - 'DataFrameGroupBy' object has no attribute 'to_frame' - 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
Pandas AttributeError: 'DataFrame' object has no attribute 'group_by'

I mean, isn't it groupby(), not group_by()?

More on reddit.com
🌐 r/learnpython
3
3
February 28, 2018
🌐
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
🌐
Quora
quora.com › How-do-you-fix-pandas-that-have-no-attribute-dataframe
How to fix pandas that have no attribute dataframe - Quora
To fix the problem, change the Python file name to another file name (not pandas or pd) or ensure that there is no file with the name pandas.py or pd.py in the current directory. ... Managed VPS Hosting Made Easy.
🌐
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. The following methods are available in both SeriesGroupBy and DataFrameGroupBy objects, but may differ slightly, usually in that the DataFrameGroupBy version usually permits the specification of an axis argument, and often an argument indicating whether to restrict application to columns of a specific data type.
🌐
CopyProgramming
copyprogramming.com › howto › error-attributeerror-dataframegroupby-object-has-no-attribute-while-groupby-functionality-on-dataframe
Python: DataFrameGroupBy Object Does Not Have Attribute Error Occurs When Using Groupby Functionality on DataFrame
April 22, 2023 - The outcome of the program is an AttributeError that indicates the inability to reach the 'drop_duplicates' attribute of ' DataFrameGroupBy' object s. Instead, it suggests using the 'apply' method. Saving groupby output content to a file with Python pandas .
Find elsewhere
🌐
Medium
paddyalton.medium.com › python-learnt-backwards-3482f52f0eb4
Python, learnt backwards. A tutorial for near-beginners | by Paddy Alton | Medium
December 22, 2022 - In the case of the linked question, the poster was confused because the DataFrame.groupby method doesn’t return a DataFrame object at all; it returns a DataFrameGroupBy object, a completely different class with different methods. Armed with the above details, the poster may deduce that they need to use one of those methods to convert back to a DataFrame … which does have a to_csv method. The right thing would then be to consult the documentation. Secondly, let’s examine this one — AttributeError: 'numpy.ndarray' object has no attribute 'append'.
🌐
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

🌐
CopyProgramming
copyprogramming.com › howto › dataframe-object-has-no-attribute-shape
Python: Attribute shape is missing in the Dataframe object
June 7, 2023 - Attribute 'shape' is not available in the 'DataFrameGroupBy' object, Python: Strategies for Resolving AttributeError: 'DataFrame' object does not have the attribute 'flatten', Failure in writing esri-shape file using GeoPandas: AttributeError encountered - 'DataFrame' object does not possess 'to_file' attribute
🌐
Databricks Community
community.databricks.com › t5 › data-engineering › attributeerror-dataframe-object-has-no-attribute › td-p › 61132
AttributeError: 'DataFrame' object has no attribut... - Databricks Community - 61132
February 19, 2024 - Hello, I have some trouble deduplicating rows on the "id" column, with the method "dropDuplicatesWithinWatermark" in a pipeline. When I run this pipeline, I get the error message: "AttributeError: 'DataFrame' object has no attribute 'dropDuplicatesWithinWatermark'" Here is part of the code: @dl...
Top answer
1 of 1
5

I think I understood what you wanted to do and what you did not understand (mainly about the way to modifiy objects with pandas). I assume that you wanted to:

  1. compute your aggregation by payment date in data
  2. and then set its index to 'Payment date' field

Short answer: if you want to have this result into data, simply execute:

data = data.groupby('Payment date ')['Payment amount'].sum().to_frame()

'Payment date ' will be your new index, to_frame prevents your single column resulting dataframe to be squeezed into a pandas Series (which I think was your first intention to avoid, resetting your index to then set it back).

Let's dive into your code.

First line

data = data.groupby('Payment Date ')

First line is ok, but might not do exactly what you want. You are taking data, which I assume is a pandas DataFrame and reaffect it a pandas DataFrameGroupBy object. This kind of object does not hold any data, you can see it simply as a mapping between index(s) of your original DataFrame and associated groups (here, payment dates).

Anyway, you got your groupby object into data.

Second line

data['Payment Amount '].sum().reset_index()

This line does nothing. It shows the result of the computation in your Jupyter notebook, but nothing has been changed in data. data is still the same DataFrameGroupBy object.

Third line

data = data.set_index('Payment Date ', inplace  = True)

An exception is raised, saying that a DataFrameGroupBy objet has no set_index method. This is because data has not been changed by your second line of code. Even so, I would encourage you to avoid using inplace=True anytime in your code. You should always go with explicit reassignements.

Your code could look like (if you don't like the short answer above):

data = data.groupby('Payment date ')
data = data['Payment amount'].sum().reset_index()
data = data.set_index('Payment date ')  # No inplace=True!
🌐
GitHub
github.com › nalepae › pandarallel › issues › 255
AttributeError: 'DataFrameGroupBy' object has no attribute 'parallel_apply' · Issue #255 · nalepae/pandarallel
November 11, 2023 - AttributeError: 'DataFrameGroupBy' object has no attribute 'parallel_apply' Write here the observed behavior
Author   beyondguo
🌐
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!