我有一个12000行3列的数据集;我想从这个数据集中随机选择600行,并修改它们的值(特定列的值(。python中有什么方法可以做到这一点吗?
你可以试试这样的东西:
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()
借助这个:
df.sample(n=600)
# and then you can call the colum you want by using
pandas.loc function and edit according to your wish