grouped = s.groupby(s)
Or:
grouped = s.groupby(lambda x: s[x])
Answer from user1201614 on Stack OverflowPandas
pandas.pydata.org › docs › reference › api › pandas.Series.groupby.html
pandas.Series.groupby — pandas 3.0.3 documentation
An exception to this is that pandas ... user guide linked above for more details. ... >>> ser = pd.Series([390., 350., 30., 20.], ... index=['Falcon', 'Falcon', 'Parrot', 'Parrot'], ......
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.Series.groupby.html
pandas.Series.groupby — pandas 3.0.1 documentation
An exception to this is that pandas ... user guide linked above for more details. ... >>> ser = pd.Series([390., 350., 30., 20.], ... index=['Falcon', 'Falcon', 'Parrot', 'Parrot'], ......
Videos
44:17
The Complete Guide to Python Pandas Groupby - YouTube
05:58
How to use Pandas Groupby Like a Pro! - YouTube
19:03
How to use the Pandas GroupBy function | Pandas tutorial - YouTube
11:05
Group By and Aggregate Functions in Pandas | Python Pandas Tutorials ...
15:50
Group By Function | Pandas Data Analysis Tutorial #1 | Retail Example ...
w3resource
w3resource.com › pandas › series › series-groupby.php
Pandas: Series - groupby() function - w3resource
import numpy as np import pandas as pd df = pd.DataFrame({'Animal': ['Tiger', 'Tiger', 'Dog', 'Dog'], 'Max Speed': [270., 260., 36., 32.]}) df.groupby(['Animal']).mean() ... import numpy as np import pandas as pd arrays = [['Tiger', 'Tiger', 'Dog', 'Dog'], ['Captive', 'Wild', 'Captive', 'Wild']] index = pd.MultiIndex.from_arrays(arrays, names=('Animal', 'Type')) df = pd.DataFrame({'Max Speed': [280., 250., 30., 20.]}, index=index) df
pandas
pandas.pydata.org › pandas-docs › dev › reference › api › pandas.Series.groupby.html
pandas.Series.groupby — pandas ain documentation
An exception to this is that pandas ... linked above for more details. ... >>> ser = pd.Series( ... [390.0, 350.0, 30.0, 20.0], ... index=["Falcon", "Falcon", "Parrot", "Parrot"], ......
Pandas
pandas.pydata.org › pandas-docs › version › 1.5 › reference › api › pandas.Series.groupby.html
pandas.Series.groupby — pandas 1.5.2 documentation
Returns a groupby object that contains information about the groups. ... Convenience method for frequency conversion and resampling of time series. ... See the user guide for more detailed usage and examples, including splitting an object into groups, iterating through groups, selecting a group, aggregation, and more. ... >>> ser = pd.Series([390., 350., 30., 20.], ... index=['Falcon', 'Falcon', 'Parrot', 'Parrot'], name="Max Speed") >>> ser Falcon 390.0 Falcon 350.0 Parrot 30.0 Parrot 20.0 Name: Max Speed, dtype: float64 >>> ser.groupby(["a", "b", "a", "b"]).mean() a 210.0 b 185.0 Name: Max Speed, dtype: float64 >>> ser.groupby(level=0).mean() Falcon 370.0 Parrot 25.0 Name: Max Speed, dtype: float64 >>> ser.groupby(ser > 100).mean() Max Speed False 25.0 True 370.0 Name: Max Speed, dtype: float64
Pandas
pandas.pydata.org › docs › reference › api › pandas.core.groupby.SeriesGroupBy.indices.html
pandas.core.groupby.SeriesGroupBy.indices — pandas 2.3.3 documentation
Examples · For SeriesGroupBy: >>> lst = ['a', 'a', 'b'] >>> ser = pd.Series([1, 2, 3], index=lst) >>> ser a 1 a 2 b 3 dtype: int64 >>> ser.groupby(level=0).indices {'a': array([0, 1]), 'b': array([2])} For DataFrameGroupBy: >>> data = [[1, 2, 3], [1, 5, 6], [7, 8, 9]] >>> df = pd.DataFrame(data, ...
Pandas
pandas.pydata.org › docs › reference › groupby.html
GroupBy — pandas 3.0.2 documentation - PyData |
pandas.api.typing.DataFrameGroupBy and pandas.api.typing.SeriesGroupBy instances are returned by groupby calls pandas.DataFrame.groupby() and pandas.Series.groupby() respectively · DataFrameGroupBy.__iter__()
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.groupby.html
pandas.DataFrame.groupby — pandas 3.0.3 documentation
DataFrame.groupby(by=None, level=None, *, as_index=True, sort=True, group_keys=True, observed=True, dropna=True)[source]# Group DataFrame using a mapper or by a Series of columns.
Pandas
pandas.pydata.org › docs › user_guide › groupby.html
Group by: split-apply-combine — pandas 3.0.3 documentation
Another aggregation example is to compute the size of each group. This is included in GroupBy as the size method. It returns a Series whose index consists of the group names and the values are the sizes of each group.
Pandas
pandas.pydata.org › docs › reference › api › pandas.core.groupby.SeriesGroupBy.count.html
pandas.core.groupby.SeriesGroupBy.count — pandas 2.3.3 documentation
Apply a function groupby to each row or column of a DataFrame. Examples · For SeriesGroupBy: >>> lst = ['a', 'a', 'b'] >>> ser = pd.Series([1, 2, np.nan], index=lst) >>> ser a 1.0 a 2.0 b NaN dtype: float64 >>> ser.groupby(level=0).count() a 2 b 0 dtype: int64 ·
Statology
statology.org › home › pandas: how to group by index and perform calculation
Pandas: How to Group By Index and Perform Calculation
December 18, 2023 - #find count of unique values of 'points' grouped by 'position index column df.groupby(['team', 'points'])['rebounds'].nunique() team points A 7 1 16 1 19 1 B 8 2 9 1 10 1 Name: rebounds, dtype: int64 · The following tutorials explain how to perform other common operations in pandas:
Linux find Examples
queirozf.com › entries › pandas-dataframe-groupby-examples
Pandas DataFrame: GroupBy Examples
October 18, 2020 - import pandas as pd df = pd.DataFrame({ 'value':[20,22,32,111,33,100,99], 'product':['table','chair','chair','mobile phone','table','mobile phone','table'] }) # you can define a function like this or use a lambda function def count_even_numbers(series): return len([elem for elem in series if ...
Pandas
pandas.pydata.org › docs › dev › reference › api › pandas.Series.groupby.html
pandas.Series.groupby — pandas 3.0.0.dev0+2709.g415830fc8f documentation
An exception to this is that pandas ... user guide linked above for more details. ... >>> ser = pd.Series([390., 350., 30., 20.], ... index=['Falcon', 'Falcon', 'Parrot', 'Parrot'], ......
Top answer 1 of 4
70
From version 0.20.1 it is simplier:
Strings passed to DataFrame.groupby() as the by parameter may now reference either column names or index level names
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
index = pd.MultiIndex.from_arrays(arrays, names=['first', 'second'])
df = pd.DataFrame({'A': [1, 1, 1, 1, 2, 2, 3, 3],
'B': np.arange(8)}, index=index)
print (df)
A B
first second
bar one 1 0
two 1 1
baz one 1 2
two 1 3
foo one 2 4
two 2 5
qux one 3 6
two 3 7
print (df.groupby(['second', 'A']).sum())
B
second A
one 1 2
2 4
3 6
two 1 4
2 5
3 7
2 of 4
46
this should work:
>>> df = pd.DataFrame(np.random.randint(0,5,(6, 2)), columns=['col1','col2'])
>>> df['ind1'] = list('AAABCC')
>>> df['ind2'] = range(6)
>>> df.set_index(['ind1','ind2'], inplace=True)
>>> df
col1 col2
ind1 ind2
A 0 3 2
1 2 0
2 2 3
B 3 2 4
C 4 3 1
5 0 0
>>> df.groupby([df.index.get_level_values(0),'col1']).count()
col2
ind1 col1
A 2 2
3 1
B 2 1
C 0 1
3 1
I had the same problem using one of the columns from multiindex. with multiindex, you cannot use df.index.levels[0] since it has only distinct values from that particular index level and will be most likely of different size than whole dataframe...
check http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Index.get_level_values.html - get_level_values "Return vector of label values for requested level, equal to the length of the index"
Built In
builtin.com › data-science › pandas-groupby
Pandas Groupby: 5 Methods to Know in Python | Built In
See last row of each group in Pandas groupby. | Image: Suraj Gurav · .first() and .last() returned the first and the last row once all the rows were grouped under each Product_Category. You can also extract a row at any other position, as well. For example, extracting the fourth row in each group is also possible using function .nth(). ... Remember, indexing in Python starts with zero, therefore, when you say .nth(3), you are actually accessing the fourth row.
GeeksforGeeks
geeksforgeeks.org › how-to-do-groupby-on-a-multiindex-in-pandas
How to do groupby on a multiindex in Pandas? - GeeksforGeeks
June 9, 2022 - In this article, we will be showing how to use the groupby on a Multiindex Dataframe in Pandas. In Data science when we are performing exploratory data analysis, we often use groupby to group the data of one column based on the other column. So, we are able to analyze how the data of one column is grouped or depending based upon the other column.