...

/

Solution: Looping, Aggregation, and apply() Method

Solution: Looping, Aggregation, and apply() Method

See the solution to the problems related to looping, aggregation, and the apply() method.

We'll cover the following...

Problem 1: Solution

Given the following dataset:

100 56 17
12 23 1
49 3 65
32 19 41
99 100 41

Create a DataFrame named df. Loop over each row and calculate the maximum and minimum values.

Press + to interact
import numpy as np
import pandas as pd
matrix = [(100, 56, 17),
(12, 23, 1),
(49, 3, 65),
(32, 19, 43),
(99, 100, 41)
]
# Create a DataFrame
df = pd.DataFrame(matrix, index = list('abcde'), columns = list('xyz'))
# iterate through each row and select
for i in range(len(df)):
print("max value in row", i,": ",max(df.iloc[i, 0], df.iloc[i,1], df.iloc[i, 2]))
print("min value in row", i,": ",min(df.iloc[i, 0], df.iloc[i,1], df.iloc[i, 2]))
...