Try doing this:

week_grouped = df.groupby('week')
week_grouped.sum().reset_index().to_csv('week_grouped.csv')

That'll write the entire dataframe to the file. If you only want those two columns then,

week_grouped = df.groupby('week')
week_grouped.sum().reset_index()[['week', 'count']].to_csv('week_grouped.csv')

Here's a line by line explanation of the original code:

# This creates a "groupby" object (not a dataframe object) 
# and you store it in the week_grouped variable.
week_grouped = df.groupby('week')

# This instructs pandas to sum up all the numeric type columns in each 
# group. This returns a dataframe where each row is the sum of the 
# group's numeric columns. You're not storing this dataframe in your 
# example.
week_grouped.sum() 

# Here you're calling the to_csv method on a groupby object... but
# that object type doesn't have that method. Dataframes have that method. 
# So we should store the previous line's result (a dataframe) into a variable 
# and then call its to_csv method.
week_grouped.to_csv('week_grouped.csv')

# Like this:
summed_weeks = week_grouped.sum()
summed_weeks.to_csv('...')

# Or with less typing simply
week_grouped.sum().to_csv('...')
Answer from Alex Luis Arias on Stack Overflow
Top answer
1 of 7
30

Try doing this:

week_grouped = df.groupby('week')
week_grouped.sum().reset_index().to_csv('week_grouped.csv')

That'll write the entire dataframe to the file. If you only want those two columns then,

week_grouped = df.groupby('week')
week_grouped.sum().reset_index()[['week', 'count']].to_csv('week_grouped.csv')

Here's a line by line explanation of the original code:

# This creates a "groupby" object (not a dataframe object) 
# and you store it in the week_grouped variable.
week_grouped = df.groupby('week')

# This instructs pandas to sum up all the numeric type columns in each 
# group. This returns a dataframe where each row is the sum of the 
# group's numeric columns. You're not storing this dataframe in your 
# example.
week_grouped.sum() 

# Here you're calling the to_csv method on a groupby object... but
# that object type doesn't have that method. Dataframes have that method. 
# So we should store the previous line's result (a dataframe) into a variable 
# and then call its to_csv method.
week_grouped.to_csv('week_grouped.csv')

# Like this:
summed_weeks = week_grouped.sum()
summed_weeks.to_csv('...')

# Or with less typing simply
week_grouped.sum().to_csv('...')
2 of 7
9

Group By returns key, value pairs where key is the identifier of the group and the value is the group itself, i.e. a subset of an original df that matched the key.

In your example week_grouped = df.groupby('week') is set of groups (pandas.core.groupby.DataFrameGroupBy object) which you can explore in detail as follows:

for k, gr in week_grouped:
    # do your stuff instead of print
    print(k)
    print(type(gr)) # This will output <class 'pandas.core.frame.DataFrame'>
    print(gr)
    # You can save each 'gr' in a csv as follows
    gr.to_csv('{}.csv'.format(k))

Or alternatively you can compute aggregation function on your grouped object

result = week_grouped.sum()
# This will be already one row per key and its aggregation result
result.to_csv('result.csv') 

In your example you need to assign the function result to some variable as by default pandas objects are immutable.

some_variable = week_grouped.sum() 
some_variable.to_csv('week_grouped.csv') # This will work

basically result.csv and week_grouped.csv are meant to be same

🌐
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

Discussions

pandas.core.groupby.DataFrameGroupBy to_csv method doesn't ouput csv file as expected
There was an error while loading. Please reload this page More on github.com
🌐 github.com
13
September 19, 2013
[Enhancement] Bumping pandas version from 1.5.0 to 2.x
To bump pandas version from 1.5.0 to 2.x, we have to make changes to resolve issues such as AttributeError: 'DataFrameGroupBy' object has no attribute 'mad' -> mad() is deprecate... More on github.com
🌐 github.com
18
August 31, 2023
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' More on stackoverflow.com
🌐 stackoverflow.com
df_high = df_all[['model_cd', 'modelname', 'installcost', 'yearlyupkeep', 'Efficiency']][df_all['Efficiency']=="High"] high_efficiency_model = df_high[['model_cd', 'modelname', 'installcost', 'yearlyupkeep', 'Efficiency']].groupby('Efficiency') high_efficiency_model.to_csv(index = True) keep getting an error saying : AttributeError: 'DataFrameGroupBy'
Answer to df_high = df_all[['model_cd', 'modelname', More on chegg.com
🌐 chegg.com
1
April 6, 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
🌐
GitHub
github.com › pandas-dev › pandas › issues › 4882
pandas.core.groupby.DataFrameGroupBy to_csv method doesn't ouput csv file as expected · Issue #4882 · pandas-dev/pandas
September 19, 2013 - >>> df1 = pd.DataFrame( { "Name" : ["Alice", "Bob", "Mallory", "Mallory", "Bob" , "Mallory"] , "City" : ["Seattle", "Seattle", "Portland", "Seattle", "Seattle", "Portland"] } ) >>> g1 = df1.groupby( [ "Name" ] ) >>> print g1.head() City Name Name Alice 0 Seattle Alice Bob 1 Seattle Bob 4 Seattle Bob Mallory 2 Portland Mallory 3 Seattle Mallory 5 Portland Mallory >>> g1.to_csv('out.csv') g1.to_csv('out.csv') Out[10]: Name Alice None Bob None Mallory None dtype: object
Author   c0indev3l
🌐
Brainly
brainly.com › computers and technology › high school › does a list object have an attribute 'to_csv'?
[FREE] Does a list object have an attribute 'to_csv'? - brainly.com
A list in Python does not have a 'to_csv' attribute because this method is part of the pandas library used with DataFrames. To use 'to_csv', convert your list to a DataFrame first using pd.DataFrame().
🌐
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'.
🌐
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 - week_grouped = df.groupby('week') ... df function. ... The outcome of the program is an AttributeError that indicates the inability to reach the 'drop_duplicates' attribute of ' DataFrameGroupBy' object s....
🌐
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.
Find elsewhere
🌐
GitHub
github.com › opensearch-project › opensearch-py-ml › issues › 270
[Enhancement] Bumping pandas version from 1.5.0 to 2.x · Issue #270 · opensearch-project/opensearch-py-ml
August 31, 2023 - AttributeError: 'DataFrameGroupBy' object has no attribute 'mad' -> mad() is deprecated after 1.5.0 (https://pandas.pydata.org/pandas-docs/version/1.5/reference/api/pandas.DataFrame.mad.html)(https://github.com/pandas-dev/pandas/issues/11787) AttributeError("'DataFrame' object has no attribute '_construct_axes_from_arguments'" TypeError: quantile() got an unexpected keyword argument 'numeric_only' TypeError: to_csv() got an unexpected keyword argument 'line_terminator'
Author   thanawan-atc
🌐
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
🌐
Python Forum
python-forum.io › thread-17746.html
Pandas to_csv in for loop AttributeError: 'tuple' object has no attribute 'to_csv'
Hi all, Thank you in advance for all suggestions. I am attempting to read data from a csv file and populate a field ('Result_Header') with a value. The example below is just to help me understand the concept -- the final code will actually be popula...
🌐
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
🌐
Pandas How To
pandashowto.com › pandas how to › advanced topics › resolving attributeerror: object has no attribute 'to_csv' • pandas how to
Resolving AttributeError: Object Has No Attribute 'to_csv' • Pandas How To
September 22, 2024 - A DataFrame is a two-dimensional ... To avoid the AttributeError: object has no attribute ‘to_csv’, you need to make sure that the object you are calling the to_csv method on is a DataFrame....
Top answer
1 of 5
2

"sklearn.datasets" is a scikit package, where it contains a method load_iris().

load_iris(), by default return an object which holds data, target and other members in it. In order to get actual values you have to read the data and target content itself.

Whereas 'iris.csv', holds feature and target together.

FYI: If you set return_X_y as True in load_iris(), then you will directly get features and target.

from sklearn import datasets
data,target = datasets.load_iris(return_X_y=True)
2 of 5
1

The Iris Dataset from Sklearn is in Sklearn's Bunch format:

print(type(iris))
print(iris.keys())

output:

<class 'sklearn.utils.Bunch'>
dict_keys(['data', 'target', 'target_names', 'DESCR', 'feature_names', 'filename'])

So, that's why you can access it as:

x=iris.data
y=iris.target

But when you read the CSV file as DataFrame as mentioned by you:

iris = pd.read_csv('iris.csv',header=None).iloc[:,2:4]
iris.head()

output is:

    2   3
0   petal_length    petal_width
1   1.4 0.2
2   1.4 0.2
3   1.3 0.2
4   1.5 0.2

Here the column names are '1' and '2'.

First of all you should read the CSV file as:

df = pd.read_csv('iris.csv')

you should not include header=None as your csv file includes the column names i.e. the headers.

So, now what you can do is something like this:

X = df.iloc[:, [2, 3]] # Will give you columns 2 and 3 i.e 'petal_length' and 'petal_width'
y = df.iloc[:, 4] # Label column i.e 'species'

or if you want to use the column names then:

X = df[['petal_length', 'petal_width']]
y = df.iloc['species']

Also, if you want to convert labels from string to numerical format use sklearn LabelEncoder

from sklearn import preprocessing
le = preprocessing.LabelEncoder()
y = le.fit_transform(y)