Menu
Coddy logo textTech

Different Aggregations

Lesson 16 of 19 in Coddy's Pandas Analytics course.

When using .sum() or any other similar method, it calculates the sum across all numeric columns. We can also specify for each column which calculation to do:

df.groupby('column_name').agg({'column_1': 'sum', 'column_2': 'mean'})
challenge icon

Challenge

Easy

The CSV file visits.csv contains information about how many visits were made at a particular location at a particular time.

Here is the first 5 lines of the file:

location_id,visits,timestamp
8,1771,27498
9,4187,79919
0,4959,54228
6,7721,41588

Create a dataframe with the following columns: sum_visits and mean_timestamp, and their corresponding statistical calculations.

Make sure sum_visits is before mean_timestamp. .agg({column_1: '...', colunm_2: '...'}) it not the same as .agg({column_2: '...', colunm_1: '...'}). This is because the columns order is reversed.

Save the result to df.

Try it yourself

# pandas as pd is already imported
df = pd.read_csv("./visits.csv")
# Write your code below

All lessons in Pandas Analytics