I have a data set which has "Speed" as one of the columns (features). The column contains both zero and non-zero values. I want to randomly set 10% of the non-zero values to zeros. This will change the corresponding "class" label to zeros. I mean any value set to zero, its corresponding class value will be zero as well. I have done this but it is give me errors. Though due to error, I cannot tell it will give me the update/result I want.
file_path = 'Processed_data/data1.csv' df = pd.read_csv(file_path) per_change = 0.1 attr = 'Speed' target = 'Class' df_spd = df[df['Speed'] > 0.] num_rows_to_change = int(df.shape * per_change) num_with_zero_initial = df[df[attr] == 0].shape assert df_spd.shape > num_rows_to_change, \ 'Number of rows with non-zero speed is less than 10% of the original dataset.' df_update = df_spd.sample(num_rows_to_change) df_update[attr] = 0. df_update[target] = 0. df.update(df_update) update_list = df_update.index.tolist() num_with_zero_final = df[df['Speed'] == 0].shape assert num_with_zero_final == num_with_zero_initial + num_rows_to_change, \ 'Number of rows needed to change not equal to number of rows changed.' df.to_csv('changed.csv')