You can download this code by clicking the button below.
This code is now available for download.
This function randomly selects rows from a given Pandas DataFrame based on a specified number of rows. It is useful for data sampling, testing, and other scenarios.
Technology Stack : Pandas, Numpy
Code Type : Pandas DataFrame operation
Code Difficulty : Intermediate
def random_index_dataframe(df, n):
"""
Selects random rows from a Pandas DataFrame based on a given number of rows.
"""
import pandas as pd
import numpy as np
if not isinstance(df, pd.DataFrame):
raise ValueError("The input must be a Pandas DataFrame.")
if not isinstance(n, int) or n <= 0:
raise ValueError("The number of rows must be a positive integer.")
return df.sample(n)