title: Can I apply lambda function to df column over specific rows?
body: <p>New to python, so bear with me!</p>
  
  <p>I have a function that applies random values to different columns in my df (depending on the column name; each column has specific random value range). For one specific function, I want to apply it to just half of my column. I attempted working with just my even rows to deal with this, but it seems it's not working. When I print out the first few rows, I can tell the column did not update. Would appreciate any help!</p>
  
  <p>Here's what I've attempted:</p>
  
  <pre><code>    for col in df.columns:
          if 'shirt' in col:
              df[col] = df[col].apply(lambda x: np.random.randint(0,5))
          elif 'pants' in col:
              df[col] = df[col].apply(lambda x: np.random.randint(0,10))
          elif 'sweater' in col:
              df[col] = df[col].apply(lambda x: np.random.randint(0,50) if df.index.all() % 2 == 0 else np.NaN)
          else:
              pass
  </code></pre>
  
  <p>(had to add <code>.all()</code> bc I got a <code>Value Error: The truth value of an array with more than one element is ambiguous</code> error.)</p>
  
  <p>Using the index to apply it to half of my dataframe is what I can think of, but this wouldn't work if I wanted to apply that lambda function to just 60% or 80% of my column and leave the rest as nulls. </p>
  
  <p>This is what I get as a result of the code above:</p>
  
  <pre><code>    shirt_count  pant_count  sweater_count  
  0            14            3            5 
  1            18            3            7 
  2             1            3            5 
  3             7            1            9 
  4             2            3            2 
  </code></pre>
  
  <p>Would love any help as I've been staring at my screen for hours! </p>