1

I am trying an algorithm to test the sensitivity of a simple machine learning model. For that I need to change all the values of one column to a value that has occured at least once in that column, while keeping the other columns unchanged. This will be then be passed into the model to make a prediction and then we repeat the procedure again for another value that has occured (until all values have been tried). And this we do for all columns.

Now, I would like to know if there is an efficient way to change the values of an entire column to a specific value?

Now, I thought about creating a for loop to go over the columns and nest within that a for loop that goes over the rows (to get a value from that column to change all other values to). I am a bit new to DataFrames so all I can think of is yet another for loop to go over all rows once again to change them one by one. But then I would have 3 nested for loops and second for loop already is not efficient since there are probably values that occurs multiple times, so I really want to avoid this 3rd for loop. Is there a quick way to change the whole column of a DataFrame to a certain value?

Sanji1P
  • 53
  • 6
  • 1
    `df[column_name] = value` is very fast. it's a bit unclear how you're determining what the value should be - but something like `df[column_name] = df[column_name].dropna().iloc[0]` would do the trick and is reasonably efficient. if your data is really huge you may want to try something else, but it would help to have details and a complete [mre]. – Michael Delgado Oct 27 '22 at 20:38
  • Please provide enough code so others can better understand or reproduce the problem. – Community Oct 27 '22 at 21:59

1 Answers1

1

There is another question: Set value to an entire column of a pandas dataframe.

You can use:

df = df.assign(column_name=value)
Tobotis
  • 96
  • 1
  • 7