Manish Saraswat kindly answered my question in the comments.

g1['group'].apply(pd.DataFrame)
Answer from Sean McCarthy on Stack Overflow
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.groupby.html
pandas.Series.groupby — pandas 3.0.2 documentation
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. levelint, level name, or sequence of such, default None · If the axis is a MultiIndex (hierarchical), group by a particular level or levels. Do not specify both by and level. ... Return object with group labels as the index. Only relevant for DataFrame ...
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › convert groupby output from series to dataframe?
Convert GroupBy output from Series to DataFrame? - Spark By {Examples}
December 11, 2024 - How to Convert a GroupBy output from Series to Pandas DataFrame? Performing aggregation function after groupby() function returns a pandas Series hence
🌐
Pandas
pandas.pydata.org › docs › dev › reference › api › pandas.core.groupby.SeriesGroupBy.transform.html
pandas.core.groupby.SeriesGroupBy.transform — pandas 3.0.0.dev0+2098.g9c5b9ee823 documentation
The group data and group index will be passed as numpy arrays to the JITed user defined function, and no alternative execution attempts will be tried. Changed in version 1.3.0: The resulting dtype will reflect the return value of the passed func, see the examples below. Changed in version 2.0.0: When using .transform on a grouped DataFrame and the transformation function returns a DataFrame, pandas now aligns the result’s index with the input’s index.
🌐
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.Series.groupby.html
pandas.Series.groupby — pandas 3.0.1 documentation
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. levelint, level name, or sequence of such, default None · If the axis is a MultiIndex (hierarchical), group by a particular level or levels. Do not specify both by and level. ... Return object with group labels as the index. Only relevant for DataFrame ...
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.core.groupby.SeriesGroupBy.get_group.html
pandas.core.groupby.SeriesGroupBy.get_group — pandas 2.3.1 documentation
Back to top · GitHub · Twitter · Mastodon · SeriesGroupBy.get_group(name, obj=None)[source]# Construct DataFrame from group with provided name. Parameters: nameobject · The name of the group to get as a DataFrame. objDataFrame, default None · The DataFrame to take the DataFrame out of.
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 0.25.3 › reference › api › pandas.Series.groupby.html
pandas.Series.groupby — pandas 0.25.3 documentation
>>> arrays = [['Falcon', 'Falcon', 'Parrot', 'Parrot'], ... ['Captive', 'Wild', 'Captive', 'Wild']] >>> index = pd.MultiIndex.from_arrays(arrays, names=('Animal', 'Type')) >>> df = pd.DataFrame({'Max Speed': [390., 350., 30., 20.]}, ...
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.core.groupby.SeriesGroupBy.aggregate.html
pandas.core.groupby.SeriesGroupBy.aggregate — pandas 2.3.3 documentation
Back to top · Search Ctrl+K · Choose version · GitHub · Twitter · Mastodon · Search Ctrl+K · Choose version · GitHub · Twitter · Mastodon · SeriesGroupBy.aggregate(func=None, *args, engine=None, engine_kwargs=None, **kwargs)[source]# Aggregate using one or more operations over the specified axis.
🌐
w3resource
w3resource.com › pandas › series › series-groupby.php
Pandas: Series - groupby() function - w3resource
This can be used to group large amounts of data and compute operations on these groups · Syntax: Series.groupby(self, by=None, axis=0, level=None, as_index=True, sort=True, group_keys=True, squeeze=False, observed=False, **kwargs) Parameters: Returns: DataFrameGroupBy or SeriesGroupBy Depends on the calling object and returns groupby object that contains information about the groups.
Find elsewhere
🌐
Saturn Cloud
saturncloud.io › blog › how-to-convert-pandascoregroupbyseriesgroupby-to-dataframe
How to Convert PandascoregroupbySeriesGroupBy to DataFrame | Saturn Cloud Blog
August 25, 2023 - In the code above, we use the pd.concat() method to concatenate the list of DataFrames stored in the variable df_list into a single DataFrame, which we store in the variable df_concat. The final step in converting a SeriesGroupBy to a DataFrame is to reset the index of the concatenated DataFrame using the .reset_index() method.
Top answer
1 of 13
709

g1 here is a DataFrame. It has a hierarchical index, though:

In [19]: type(g1)
Out[19]: pandas.core.frame.DataFrame

In [20]: g1.index
Out[20]: 
MultiIndex([('Alice', 'Seattle'), ('Bob', 'Seattle'), ('Mallory', 'Portland'),
       ('Mallory', 'Seattle')], dtype=object)

Perhaps you want something like this?

In [21]: g1.add_suffix('_Count').reset_index()
Out[21]: 
      Name      City  City_Count  Name_Count
0    Alice   Seattle           1           1
1      Bob   Seattle           2           2
2  Mallory  Portland           2           2
3  Mallory   Seattle           1           1

Or something like:

In [36]: DataFrame({'count' : df1.groupby( [ "Name", "City"] ).size()}).reset_index()
Out[36]: 
      Name      City  count
0    Alice   Seattle      1
1      Bob   Seattle      2
2  Mallory  Portland      2
3  Mallory   Seattle      1
2 of 13
193

I want to slightly change the answer given by Wes, because version 0.16.2 requires as_index=False. If you don't set it, you get an empty dataframe.

Source:

Aggregation functions will not return the groups that you are aggregating over if they are named columns, when as_index=True, the default. The grouped columns will be the indices of the returned object.

Passing as_index=False will return the groups that you are aggregating over, if they are named columns.

Aggregating functions are ones that reduce the dimension of the returned objects, for example: mean, sum, size, count, std, var, sem, describe, first, last, nth, min, max. This is what happens when you do for example DataFrame.sum() and get back a Series.

nth can act as a reducer or a filter, see here.

import pandas as pd

df1 = pd.DataFrame({"Name":["Alice", "Bob", "Mallory", "Mallory", "Bob" , "Mallory"],
                    "City":["Seattle","Seattle","Portland","Seattle","Seattle","Portland"]})
print df1
#
#       City     Name
#0   Seattle    Alice
#1   Seattle      Bob
#2  Portland  Mallory
#3   Seattle  Mallory
#4   Seattle      Bob
#5  Portland  Mallory
#
g1 = df1.groupby(["Name", "City"], as_index=False).count()
print g1
#
#                  City  Name
#Name    City
#Alice   Seattle      1     1
#Bob     Seattle      2     2
#Mallory Portland     2     2
#        Seattle      1     1
#

EDIT:

In version 0.17.1 and later you can use subset in count and reset_index with parameter name in size:

print df1.groupby(["Name", "City"], as_index=False ).count()
#IndexError: list index out of range

print df1.groupby(["Name", "City"]).count()
#Empty DataFrame
#Columns: []
#Index: [(Alice, Seattle), (Bob, Seattle), (Mallory, Portland), (Mallory, Seattle)]

print df1.groupby(["Name", "City"])[['Name','City']].count()
#                  Name  City
#Name    City                
#Alice   Seattle      1     1
#Bob     Seattle      2     2
#Mallory Portland     2     2
#        Seattle      1     1

print df1.groupby(["Name", "City"]).size().reset_index(name='count')
#      Name      City  count
#0    Alice   Seattle      1
#1      Bob   Seattle      2
#2  Mallory  Portland      2
#3  Mallory   Seattle      1

The difference between count and size is that size counts NaN values while count does not.

🌐
Pandas
pandas.pydata.org › docs › reference › groupby.html
GroupBy — pandas 3.0.2 documentation - PyData |
Back to top · Search Ctrl+K · Choose version · GitHub · X · Mastodon · Search Ctrl+K · Choose version · GitHub · X · Mastodon · pandas.api.typing.DataFrameGroupBy and pandas.api.typing.SeriesGroupBy instances are returned by groupby calls pandas.DataFrame.groupby() and pandas.Series.groupby() respectively.
🌐
Dask
docs.dask.org › en › stable › generated › dask.dataframe.groupby.SeriesGroupBy.var.html
dask.dataframe.groupby.SeriesGroupBy.var — Dask documentation
Apply a function groupby to each row or column of a DataFrame. Examples · For SeriesGroupBy: >>> lst = ['a', 'a', 'a', 'b', 'b', 'b'] >>> ser = pd.Series([7, 2, 8, 4, 3, 3], index=lst) >>> ser a 7 a 2 a 8 b 4 b 3 b 3 dtype: int64 >>> ser.groupby(level=0).var() a 10.333333 b 0.333333 dtype: ...
🌐
AMIS Technology
technology.amis.nl › home › data analytics › convert groupby result on pandas data frame into a data frame using …. to_frame()
Convert Groupby Result on Pandas Data Frame into a Data Frame using …. to_frame() - Conclusion AMIS Technology Blog
October 11, 2019 - That you can look for in the docs, no Stackoverflow and in many blog articles. After I have used groupby on a Data Frame, instead of getting a Series result, I would like to turn the result into a new Data Frame [to continue my manipulation, querying, visualization etc.]. I feel at home in Data Frames and so do my tools and libraries.
🌐
Dask
docs.dask.org › en › latest › generated › dask.dataframe.groupby.SeriesGroupBy.aggregate.html
dask.dataframe.groupby.SeriesGroupBy.aggregate
Larger-than-memory execution for single machines, allowing you to process data that is larger than your available RAM ... Dask Dataframes are similar in this regard to Apache Spark, but use the familiar pandas API and memory model.
🌐
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.core.groupby.SeriesGroupBy.transform.html
pandas.core.groupby.SeriesGroupBy.transform — pandas 2.3.3 documentation
The group data and group index will be passed as numpy arrays to the JITed user defined function, and no alternative execution attempts will be tried. Changed in version 1.3.0: The resulting dtype will reflect the return value of the passed func, see the examples below. Changed in version 2.0.0: When using .transform on a grouped DataFrame and the transformation function returns a DataFrame, pandas now aligns the result’s index with the input’s index.
🌐
Statology
statology.org › home › how to convert pandas groupby output to dataframe
How to Convert Pandas GroupBy Output to DataFrame
March 31, 2022 - import pandas as pd #create DataFrame df = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'], 'position': ['G', 'G', 'F', 'C', 'G', 'F', 'F', 'F'], 'points': [5, 7, 7, 10, 12, 22, 15, 10]}) #view DataFrame print(df) team position points 0 A G 5 1 A G 7 2 A F 7 3 A C 10 4 B G 12 5 B F 22 6 B F 15 7 B F 10 · We can use the following syntax to count the number of players, grouped by team and position:
🌐
Dask
docs.dask.org › en › latest › generated › dask.dataframe.groupby.SeriesGroupBy.transform.html
dask.dataframe.groupby.SeriesGroupBy.transform — Dask documentation
If not provided, dask will try to infer the metadata. This may lead to unexpected results, so providing meta is recommended. For more information, see dask.dataframe.utils.make_meta. Returns · appliedSeries or DataFrame depending on columns keyword · previous · dask.dataframe.groupby.SeriesGroupBy.rolling ·