Editing a value in a DataFrame
<p>I'm trying to:</p>

<p>Import a CSV of UPC codes into a dataframe. If the UPC code is 11 characters , append '0' to it. Ex: 19962123818  --> 019962123818</p>

<p>This is the code:</p>

<pre><code> #check UPC code length. If 11 characters, adds '0' before. If &lt; 11 or &gt; 13, throws Error
for index, row in clean_data.iterrows():
    if len(row['UPC']) == 11:
        row['UPC'] = ('0' + row['UPC'])
        #clean_data.set_value(row, 'UPC',('0' + (row['UPC']))
        print ("Edited UPC:", row['UPC'], type(row['UPC']))
    if len(row['UPC']) &lt; 11 or len(row['UPC']) &gt; 13:
        print ('Error, UPC length &lt; 11 or &gt; 13:')
        print ("Error in UPC:", row['UPC'])
        quit()
</code></pre>

<p>However, when I print the data, the original value is not edited:</p>

<p><a href="https://i.stack.imgur.com/xMad7.png" rel="nofollow"><img src="https://i.stack.imgur.com/xMad7.png" alt="enter image description here"></a></p>

<p>Does anyone know what is causing this issue?</p>

<p>I tried the set_value method as mentioned in other posts, but it didn't work.</p>

<p>Thanks!</p>

<hr>

<p>Thanks for the vectorized approach, much cleaner! However, I get the following error, and the value is still not updating:</p>

<p><a href="https://i.stack.imgur.com/IjFSt.png" rel="nofollow"><img src="https://i.stack.imgur.com/IjFSt.png" alt="enter image description here"></a></p>

<p>I finally fixed it. Thanks again for the vectorized idea. If anyone has this issue in the future, here's the code I used. Also, see <a href="http://stackoverflow.com/questions/13842088/set-value-for-particular-cell-in-pandas-dataframe">this post</a> for more info.</p>

<pre><code>UPC_11_char = clean_data.UPC.astype(str).str.len() == 11
clean_data.ix[UPC_11_char, 'UPC'] = '0' + clean_data[UPC_11_char]['UPC'].astype(str)

print clean_data[UPC_11_char]['UPC']
</code></pre>

python csv pandas dataframe