Aan de slagGa gratis aan de slag

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()

Deze oefening maakt deel uit van de cursus

Writing Efficient Code with pandas

Cursus bekijken

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# 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))
Code bewerken en uitvoeren