Get startedGet started for free

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

This exercise is part of the course

Writing Efficient Code with pandas

View Course

Hands-on interactive exercise

Have a go at this exercise by completing this sample 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))
Edit and Run Code