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!
Answer from Pierre Massé on Stack Overflow
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!
🌐
Stack Overflow
stackoverflow.com › questions › 76699686 › dataframegroupby-object-has-no-attribute-reset-index
python - DataFrameGroupBy' object has no attribute 'reset_index'? - Stack Overflow
If you used groupby and then reset the index without any operation your output dataframe would be identical to your input dataframe. You need to perform an operation in between, e.g. count() to get the number of entries per ['hour', 'dt']: df_new ...
Discussions

python - AttributeError: 'list' object has no attribute 'reset_index' - Stack Overflow
I'm pretty darn new to pandas and I'm having issues with using group by. I'm trying to group by e.g., car model and car values. I wanted it to output the average or mean price for each car model.. More on stackoverflow.com
🌐 stackoverflow.com
python - Error 'AttributeError: 'DataFrameGroupBy' object has no attribute' while groupby functionality on dataframe - Stack Overflow
The problem is it not identifying the NEWS_SENTIMENT_DAILY_AVG column. Error message - AttributeError: 'DataFrameGroupBy' object has no attribute 'NEWS_SENTIMENT_DAILY_AVG' ... Reset_index works for index, not columns... More on stackoverflow.com
🌐 stackoverflow.com
python 3.x - AttributeError: Cannot access callable attribute 'reset_index' of 'DataFrameGroupBy' objects, try using the 'apply' method - Stack Overflow
I am very new to pandas and trying to use groupby. I have a df with multiple columns. I want to groupby a particular column and then sort each group based on a different column. I want to groupby c... More on stackoverflow.com
🌐 stackoverflow.com
Errors after deploying the app
Hi I created my first app using streamlit. It works without any errors in my own pc (Python 3.9.7) but when I deploy it on streamlit cloud (Python 3.9) I get this error: AttributeError: 'DataFrameGroupBy' object has no attribute 'value_counts' The line which this error refers to is: df_grouped ... More on discuss.streamlit.io
🌐 discuss.streamlit.io
1
1
March 2, 2022
🌐
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
🌐
Stack Overflow
stackoverflow.com › questions › 56213580 › attributeerror-list-object-has-no-attribute-reset-index
python - AttributeError: 'list' object has no attribute 'reset_index' - Stack Overflow
I keep getting the error: AttributeError: 'list' object has no attribute 'reset_index' ... But it was throwing a: Cannot access callable attribute 'groupby' of 'DataFrameGroupBy' objects, try using the 'apply' method
🌐
Stack Overflow
stackoverflow.com › questions › 46534653 › error-attributeerror-dataframegroupby-object-has-no-attribute-while-groupby
python - Error 'AttributeError: 'DataFrameGroupBy' object has no attribute' while groupby functionality on dataframe - Stack Overflow
The problem is it not identifying the NEWS_SENTIMENT_DAILY_AVG column. Error message - AttributeError: 'DataFrameGroupBy' object has no attribute 'NEWS_SENTIMENT_DAILY_AVG' ... Reset_index works for index, not columns...
🌐
Kaggle
kaggle.com › questions-and-answers › 511815
Convert DataFrameGroupBy object to a DataFrame
Checking your browser before accessing www.kaggle.com · Click here if you are not automatically redirected after 5 seconds
Find elsewhere
🌐
GitHub
gist.github.com › conormm › fd8b1980c28dd21cfaf6975c86c74d07
R to Python: Data wrangling with dplyr and pandas · GitHub
That would be the way to do it with .apply(). The problem is, though, that it's a bit unsafe since using .reset_index() means that you're assigning back to the data frame without keeping track of the index.
🌐
Kaggle
kaggle.com › code › hashbanger › grouping-sorting-in-pandas
Checking your browser - reCAPTCHA
Checking your browser before accessing www.kaggle.com · Click here if you are not automatically redirected after 5 seconds
🌐
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 . week_grouped.reset_index().to_c...
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 1.1 › reference › groupby.html
GroupBy — pandas 1.1.5 documentation
The following methods are available only for DataFrameGroupBy objects. pandas.api.indexers.VariableOffsetWindowIndexer.get_window_bounds pandas.core.groupby.GroupBy.__iter__
🌐
Reddit
reddit.com › r/learnpython › pandas set_index error with calplot
r/learnpython on Reddit: Pandas set_index error with calplot
March 28, 2023 -

I have a dataframe that I've moved down to two columns 'last date read' and 'read count'. I formatted it to %Y-%m and then grouped it using the dates and summed the total amount of books read.

I wanted to plot it using month_calplot from plotly_calplot but I get the following error and I'm not sure where to go from here:

Error:

line 247, in month_calplot
    gData = data.set_index(x)[y].groupby(Grouper(freq="M")).sum()
  File "C:\Python\Python310\lib\site-packages\pandas\core\generic.py", line 5575, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'Series' object has no attribute 'set_index'

Code:

df2 = df2[['last date read', 'read count']]
df2['last date read'] = pd.to_datetime(df2['last date read'])
df2['last date read'] = df2['last date read'].dt.strftime('%Y-%m')

df2 = df2.groupby(['last date read'])['read count'].sum()

print(df2)
fig3 = month_calplot(
    df2,
    x='last date read',
    y='read count',
    colorscale="Purpor",
    showscale=True,
    total_height=250,
    dark_theme=True)
🌐
iO Flood
ioflood.com › blog › pandas-reset-index
Pandas Reset Index Methods | Built-in Functions Explained
July 22, 2024 - If you see an error message saying ‘DataFrame’ object has no attribute ‘reset_index’, it usually means you’re trying to use reset_index() on an object that isn’t a DataFrame. Remember, reset_index() is a method for pandas DataFrames, not for other data types. s = pd.Series(range(3)) try: s = s.reset_index() except AttributeError as e: print(e) # Output: # 'Series' object has no attribute 'reset_index'
🌐
Apache
spark.apache.org › docs › latest › api › python › reference › pyspark.pandas › api › pyspark.pandas.DataFrame.reset_index.html
pyspark.pandas.DataFrame.reset_index — PySpark 4.1.1 documentation
Reset the index, or a level of it · For DataFrame with multi-level index, return new DataFrame with labeling information in the columns under the index names, defaulting to ‘level_0’, ‘level_1’, etc. if any are None. For a standard index, the index name will be used (if set), otherwise ...
🌐
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...
🌐
Reddit
reddit.com › r/dataanalysis › data analysis in python
r/dataanalysis on Reddit: Data Analysis in Python
December 19, 2022 -

Hello everyone!
I am a newbie at python and I looked up some problems associated with the Data Expo 2009: Airline on time data from the Harvard Dataverse (https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/HG7NV7).
I am currently working on the following question:

  1. When is the best time of day, day of the week, and time of year to fly to minimize delays?

All libraries are imported and the data is cleared up (empty columns and duplicate rows are dropped).
What I was intending to do is to plot a bar chart with "Months" on the x-axis and "ArrDelay" (arrival delays) on the y-axis.

My code looks the following way (I'm using jupyter notebook):

import pandas as pd 
dataair = pd.read_csv("/Users/issakovakamilla/Desktop/2000.csv.bz2")
dataair.dropna(how='all', axis=1, inplace=True)
dataair
import matplotlib.pyplot as plt
df = pd.DataFrame(dataair)
X = list(df.iloc[:, 0])
Y = list(df.iloc[:, 1])
plt.bar(X, Y, color='g')
plt.title("stats")
plt.xlabel("Month")
plt.ylabel("ArrDelay")
plt.show()

Somehow I don't get a plot - its been executing for 10 minutes now (I get * near input). Could anyone help me with this?

🌐
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