Aggregating & Grouping

This quiz will test your understanding of these concepts with practical examples and code-based questions.

Last Updated :
Discuss
Comments

Question 1

What does the following code return?

Python
import pandas as pd
data = {'A': [1, 2, 2], 'B': [3, 4, 5]}
df = pd.DataFrame(data)
result = df['A'].sum()
print(result)


  • 6

  • 5

  • 4

  • 3

Question 2

What does this code output?

Python
import pandas as pd
data = {'Category': ['A', 'B', 'A', 'B'], 'Values': [10, 20, 30, 40]}
df = pd.DataFrame(data)
result = df.groupby('Category')['Values'].sum()
print(result)


  • A 40

    B 60

  • Category

    A 40

    B 60


  • A 30

    B 40

  • None of the above

Question 3

Which of the following code snippets applies multiple aggregation functions?

Python
import pandas as pd 
data = {'A': [1, 2, 3], 'B': [4, 5, 6]}
df = pd.DataFrame(data)
  • df.agg(['sum', 'mean'])

  • df.groupby('A').sum()

  • df['A'].sum()

  • None of the above

Question 4

What does this code return?

Python
import pandas as pd 
data = {'A': ['foo', 'bar', 'foo', 'bar'], 'B': [1, 2, 3, 4]}
df = pd.DataFrame(data)
result = df.groupby('A')['B'].transform('mean')
print(result)


  • [2.0, 3.0, 2.0, 3.0]

  • [3.0, 2.0, 3.0, 2.0]

  • [2.0, 3.0, 3.0, 2.0]

  • None of the above

Question 5

How can you compute the range (max - min) of values for each group?

Python
import pandas as pd 
data = {'Group': ['X', 'X', 'Y', 'Y'], 'Values': [10, 20, 30, 40]}
df = pd.DataFrame(data)


  • df.groupby('Group')['Values'].apply(lambda x: x.max() - x.min())

  • df.groupby('Group').agg('range')

  • df.groupby('Group').apply(lambda x: x['Values'].range())

  • None of the above

Question 6

What does the following code output?

Python
import pandas as pd 
data = {'A': ['foo', 'foo', 'bar', 'bar'], 
        'B': ['one', 'two', 'one', 'two'], 
        'C': [1, 2, 3, 4]}
df = pd.DataFrame(data)
result = df.groupby(['A', 'B']).sum()
print(result)


  • C

    A B

    foo one 1

    two 2

    bar one 3

    two 4

  • A B

    foo one 1

    foo two 2

    bar one 3

    bar two 4

  • A B C

    foo one 1

    foo two 2

    bar one 3

    bar two 4

  • None of the above

Question 7

What does this code return?

Python
import panadas as pd 
data = {'A': ['X', 'X', 'Y', 'Y'], 'B': ['one', 'two', 'one', 'two'], 'C': [10, 20, 30, 40]}
df = pd.DataFrame(data)
result = df.pivot_table(values='C', index='A', columns='B', aggfunc='sum')
print(result)


  • B one two

    A

    X 10 20

    Y 30 40

  • B one two

    X 15.0 25.0

    Y 35.0 45.0

  • B one two

    X 1 2

    Y 3 4

  • None of the above

Question 8

How can you find the sum of values greater than 20 for each group?

Python
import panads as pd
data = {'Group': ['A', 'A', 'B', 'B'], 'Values': [10, 30, 20, 40]}
df = pd.DataFrame(data)


  • df[df['Values'] > 20].groupby('Group')['Values'].sum()

  • df.groupby('Group')['Values'].sum().apply(lambda x: x > 20)

  • df.groupby('Group').filter(lambda x: x['Values'] > 20)['Values'].sum()

  • None of the above

Question 9

What does this code output?

Python
import pandas as pd 
data = {'A': [1, 1, 2], 'B': [3, 4, 5]}
df = pd.DataFrame(data)
result = df.groupby('A').describe()
print(result)


  • B

    count mean std min 25% 50% 75% max

    A

    1 2 3.5 0.5 3 3.25 3.5 3.75 4

    2 1 5.0 NaN 5 5.00 5.0 5.00 5

  • B

    count mean std min 25% 50% 75% max

    A

    1 2 3.5 0.707 3 3.25 3.5 3.75

    2 1 5.0 NaN 5 5.00 5.0 5.00 5


  • A

    1 2 3.5 0.707 3 3.25 3.5 3.75 4

    2 1 5.0 NaN 5 5.00 5.0 5.00 5


  • None of the above

Question 10

What does this code output?

Python
import pandas as pd
data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
df = pd.DataFrame(data)
result = df[['A', 'B']].agg(['sum', 'mean'])
print(result)


  • A B

    sum 6 15

    mean 2 5

  • A B

    sum 6 15

    mean 2 5

  • A B

    sum 6.0 15.0

    mean 2.0 5.0

  • None of the above

Tags:

There are 10 questions to complete.

Take a part in the ongoing discussion