Solution: Summing and Swapping
This lesson gives a detailed review of the solution to the challenge in the previous lesson.
We'll cover the following...
Solution #
Press + to interact
def Sum_Swap(df):minm_r = np.min(df, axis = 1) # Get minimum elements from all rowsmaxm_r = np.max(df, axis = 1) # Get maximum elements from all rowsdf['row_sum'] = minm_r + maxm_r # Add the min & max values and assign them to new columnminm_c = np.min(df, axis = 0) # Get minimum elements from all columnsmaxm_c = np.max(df, axis = 0) # Get maximum elements from all columnsdf.loc['col_sum'] = minm_c + maxm_c # Add the min & max values and assign them to new rowa, b = df['row_sum'].copy(), df.loc['col_sum'].copy() # Store values of row and column in temparory variablesdf['row_sum'], df.loc['col_sum'] = b, a # Interchange the valuesreturn df# Test Codedf = pd.DataFrame(np.random.randint(1, 100, 25).reshape(5, 5))df_res = Sum_Swap(df.copy())print(df_res)
...