I have a dataset of 12000 rows and 3 columns; I want to randomly select 600 rows from this dataset and modify their values ( the values of a specific column). Is there any method in python that can do this?
CodePudding user response:
You could try something like this:
import pandas as pd
import numpy as np
import random
def random_select():
df = pd.read_csv('data.csv') # read the dataset
df = df.sample(n=600) # randomly select 600 rows
df['column'] = np.random.randint(1, 100, size=len(df)) # modify the values of a specific column
df.to_csv('data.csv', index=False) # save the modified dataset
return df # return the modified dataset
if __name__ == '__main__':
random_select()
CodePudding user response:
By the help of this :
df.sample(n=600)
# and then you can call the colum you want by using
pandas.loc function and edit according to your wish