Challenge: Summing and Swapping

In this challenge, pandas is used to sum the minimum and maximum values of the rows and columns of a DataFrame, which are then swapped.

We'll cover the following

Problem statement

We have to implement the function Sum_Swap(df). The df is the DataFrame on which operations will be performed. The task is to create a new column and a new row and then swap their values. The following steps are performed to calculate the values for the new row and column before swapping:

  1. The sum of the minimum and maximum values of each row are calculated.

  2. These values are assigned to the new column row_sum.

  3. The sum of the minimum and maximum value of each column are calculated.

  4. These values are assigned to the new row col_sum.

  5. Finally, the new row and column values are swapped.

Input

The input to the function is a DataFrame with random data. The following is just an example of what the input DataFrame would look like.

  0  1  2  3 
0 12 2  3  44
1 40 1  34 9
2 6  99 56 69
3 2  24 4  71

Output

The output is a DataFrame with a new row and column. The following is an example of what the output of the above input DataFrame would look like after summing and swapping the row and column.

        0  1  2   3   row_sum

0       12 2  3   44  42
1       40 1  34  9   100
2       6  99 56  69  59
3       2  24 4   71  80
col_sum 46 41 105 73  NaN

Coding exercise

Write your code below. It is recommended​ that you try solving the exercise yourself before viewing the solution.

Get hands-on with 1200+ tech skills courses.