Skip to content
Quantoros

Suppose you have DataFrame with 10 columns of real numbers, for example: Which column of numbers has the smallest sum?…

PythonLevel: Intermediate

Suppose you have DataFrame with 10 columns of real numbers, for example:

df = pd.DataFrame(np.random.random(size=(5, 10)), columns=list('abcdefghij'))

Which column of numbers has the smallest sum? Return that column's label.

Assume import pandas as pd and import numpy as np.

Hints
  • First define row granularity, column types and keys.
  • Check index alignment, missing values and output size after the transformation.

Answer

df = pd.DataFrame(np.random.random(size=(5, 10)), columns=list('abcdefghij'))

df.sum().idxmin()

Source and licence: 100 pandas puzzles — Alex Riley and contributors · MIT

Related questions