import pandas as pd
df = pd.DataFrame( { 'A': [1,2,3,4],
'B': [10,20,30,40],
'C': [20,40,60,80]
}, )
df['A'] = ''
print(df)
I want to set K number of column A's value into empty value, and these K values should be randomly selected. Those len(df)-K values of column A won't be affected. I wrote this function to generate the random row indexes, and then how to set the df's row values to empty for these indexes?
def random_rows(K=2, df):
col_length = df.shape[1]
row_indexes = [i for i in range(col_length)]
if col_length<K:
K = col_length
selected_row_indexes = random.sample(row_indexes, K)
return selected_row_indexes
CodePudding user response:
You can use sample
to get the random rows and loc
to modify them:
df.loc[df['A'].sample(n=2).index, 'A'] = '' # or whatever value you want