Python Data Operations 5: Group and Pivot Tables
(Using the numpy and pandas packages imported in section one.)
This fith section contains:
Groupby
Iterate through groups
Basic groupings
Advanced groupings
Group with more levels
Pivot table
Stack and Unstack
# creating test dataframe
test_df = pd.DataFrame(
[['A3', 0, -1, 0, 'si'],
['B1', 1, None, 0, 'no'],
['B3', 4, None, 0, 'no'],
['B3', 5, 1, 0, 'si'],
['A1', 4, 0, None, None],
['A3', 1, 2, 1, 'si'],
['C2', 4, 1, 1, 'no']],
columns=['Assignment', 'UserId', 'Score', 'PrevAttempts', 'Exempted'],
index=[f'R{i}' for i in range(7)]
)
test_df
Assignment UserId Score PrevAttempts Exempted
R0 A3 0 -1.0 0.0 si
R1 B1 1 NaN 0.0 no
R2 B3 4 NaN 0.0 no
R3 B3 5 1.0 0.0 si
R4 A1 4 0.0 NaN None
R5 A3 1 2.0 1.0 si
R6 C2 4 1.0 1.0 no
Groupby
The .groupby method divides the records/rows of a table in different groups, generally to
later apply certain operations separately to each group.
A grouping has two phases: