Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Advanced Aggregation [2/2] | Aggregating and Visualizing Data
Data Manipulation using pandas

bookAdvanced Aggregation [2/2]

Great! What is you need to apply certain functions to specific columns separately, like find out median of one column, and mean of another one. Surely, you can perform grouping twice. But this case is also covered by pandas and .agg() method. If you want to apply specific functions to certain columns, pass them as a dictionary {'column': 'function'}. For instance, we can calculate mean total income ('totinch') and median number of people in a dwelling ('hhsize') for each number of bedrooms.

12345678
# Importing the library import pandas as pd # Reading the file df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/f2947b09-5f0d-4ad9-992f-ec0b87cd4b3f/data4.csv') # Minimal and maximal prices for each dwelling type print(df.groupby('broomh').agg({'totinch': 'mean', 'hhsize': 'median'}))
copy

Note that if you pass dictionary as the .agg() method parameter, then there is no need to select columns after grouping.

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 4. Luku 3

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Awesome!

Completion rate improved to 2.56

bookAdvanced Aggregation [2/2]

Pyyhkäise näyttääksesi valikon

Great! What is you need to apply certain functions to specific columns separately, like find out median of one column, and mean of another one. Surely, you can perform grouping twice. But this case is also covered by pandas and .agg() method. If you want to apply specific functions to certain columns, pass them as a dictionary {'column': 'function'}. For instance, we can calculate mean total income ('totinch') and median number of people in a dwelling ('hhsize') for each number of bedrooms.

12345678
# Importing the library import pandas as pd # Reading the file df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/f2947b09-5f0d-4ad9-992f-ec0b87cd4b3f/data4.csv') # Minimal and maximal prices for each dwelling type print(df.groupby('broomh').agg({'totinch': 'mean', 'hhsize': 'median'}))
copy

Note that if you pass dictionary as the .agg() method parameter, then there is no need to select columns after grouping.

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 4. Luku 3
some-alt