You can download this code by clicking the button below.
This code is now available for download.
This function reads a CSV file and replaces the values of specified columns with random values, returning a DataFrame.
Technology Stack : Pandas, NumPy
Code Type : Function
Code Difficulty : Intermediate
def random_dataframe_from_csv(file_path, column_names):
"""
This function reads a CSV file and returns a DataFrame with random values for the specified columns.
"""
import pandas as pd
import numpy as np
df = pd.read_csv(file_path)
for column in column_names:
if column in df.columns:
df[column] = np.random.choice(df[column].unique(), len(df[column]))
else:
raise ValueError(f"Column {column} not found in the DataFrame.")
return df