Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

dataset 1

we have the following dataset, these are the results of a survey there is one line per answer, and each person participating has answered 3 questions

Loading...

objective

we want to compute, for each rating, the number of occurrences of each site among the favorite and least-liked columns

rating 1 4 2 5 3 5 4 4 dtype: int64

this is a good opportunity to see that when calling GroupBy.aggregrate, one can pass a dictionary, that says how to deal with each column

Loading...

how to understand this ? e.g. the first line means that:

(2, 2)
favorite value_counts 1.0 least-liked value_counts 4.0 Name: (3, london), dtype: float64

dataset 2

Loading...

simple groupings

we consider two groupings

for each of these groupings, compute

here again, calling aggregate with a dictionary comes in handy

Loading...
Loading...

inspecting a Groupby object

A bar 2 far 1 foo 5 dtype: int64
{'bar': [3, 5], 'far': [1], 'foo': [0, 2, 4, 6, 7]}
{('bar', 'three'): RangeIndex(start=3, stop=4, step=1), ('bar', 'two'): RangeIndex(start=5, stop=6, step=1), ('far', 'one'): RangeIndex(start=1, stop=2, step=1), ('foo', 'one'): RangeIndex(start=0, stop=12, step=6), ('foo', 'three'): RangeIndex(start=7, stop=8, step=1), ('foo', 'two'): RangeIndex(start=2, stop=6, step=2)}
Loading...
Loading...

grouping from a Series

for advanced users

{'b': [3, 5], 'f': [0, 1, 2, 4, 6, 7]}
0 f 1 f 2 f 3 b 4 f 5 b 6 f 7 f Name: A, dtype: str

using a function to decide on the grouping

this means we can group arbitrarily; for example we want to group in two categories, whether the ‘B’ column contains a o or not

<ArrowStringArray> ['one', 'two', 'three'] Length: 3, dtype: str

so this should give two groups, one the one hand the rows where B is among one and two, on the other hand the ones where B is three

B False 2 True 6 dtype: int64

using 2 functions to decide on the grouping

this can be extended arbitrarily; we could add a grouping in a separate dimension, whether the ‘A’ columns contains 'f’or not

<ArrowStringArray> ['foo', 'far', 'bar'] Length: 3, dtype: str
(False, False) 1 (False, True) 1 (True, False) 1 (True, True) 5 dtype: int64
(f in A, nope) 1 (f in A, o in B) 5 (nope, nope) 1 (nope, o in B) 1 dtype: int64

groupby using buckets

C low 3 mid 2 high 3 dtype: int64
C (0.0, 3.5] 3 (3.5, 6.5] 3 (6.5, 10.0] 2 dtype: int64