CommencerCommencer gratuitement

Random column selection

In the previous exercise, we examined two ways to select random rows from a pandas DataFrame. We can use the same functions to randomly select columns in a pandas DataFrame.

To randomly select 4 columns out of the poker dataset, you will use the following two functions:

  • The built-in pandas function .sample()
  • The NumPy random integer number generator np.random.randint()

Cet exercice fait partie du cours

Writing Efficient Code with pandas

Afficher le cours

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# Extract number of columns in dataset
D=poker_hands.shape[1]

# Select and time the selection of 4 of the dataset's columns using NumPy
np_start_time = time.time()
poker_hands.iloc[:,np.random.randint(____=____, ____=____, ____=____)]
print("Time using NymPy's random.randint(): {} sec".format(time.time() - np_start_time))
Modifier et exécuter le code