title: How do I replace cells in a Panda dataframe column based on a condition
body: <p>This post consists of two questions, touching on issues I have encountered when trying to replace elements in a Panda dataframe based on a given condition. I am new with Pandas, so any suggestions will be most helpful.</p>
  <h1>1: Modifying strings of a certain length</h1>
  <p>Consider column A in a Panda dataframe object, df:</p>
  <pre><code>SSIC
  103
  1040
  1054
  1065
  107
  </code></pre>
  <p>I want to append the integer 0 to each cell that is of length less than four. That is, I want to obtain:</p>
  <pre><code>SSIC
  0103
  1040
  1054
  1065
  0107
  </code></pre>
  <p>The values are of type float64.</p>
  <p>Currently, I have used this method:</p>
  <pre><code>SSIC1 = df['SSIC'].astype('int64').astype(str)
  
  for i,n in enumerate(SSIC1):
      if len(SSIC1[i]) == 4:
         SSIC1[i] = '0' + SSIC1[i]
  
  df['SSIC'] = SSIC1
  </code></pre>
  <p>It works, but I wonder if it is rather long-winded. Is there a more direct way to resolve this?</p>
  <h1>2: Setting with Copy Warning when using where clause</h1>
  <p>I have the following two columns in a dataframe</p>
  <pre><code>A  B
  2  1
  3  4
  4  6
  5  4
  5  2
  </code></pre>
  <p>I want to replace cells in column A that take on the value 5, with the values on the same row in B.</p>
  <p>I have used the where condition:</p>
  <pre><code>df['A']=df['A'].where(df['A'] == 5, df['B'], inplace=True)
  </code></pre>
  <p>​But it gives me the following error:</p>
  <blockquote>
  <p>SettingWithCopyWarning:  A value is trying to be set on a copy of a
  slice from a DataFrame. Try using .loc[row_indexer,col_indexer] =
  value instead</p>
  <p>See the caveats in the documentation:
  <a href="http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy" rel="nofollow noreferrer">http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy</a>
  if <strong>name</strong> == '<strong>main</strong>':</p>
  </blockquote>
  <p>This warning did not occur the first time I ran it. Any ideas why it has popped up?</p>