I have a table where the information of a field has empty values and I want to fill them with the value of the upper cell
Hi DanielFirst ,
I would probably use a python script for this and run through the records and update them based on the previously known value. Would that be an option for you?
Surely with Python, he used the following code but fill in the empty cells
previous = None
def calcowner (owner):global previewval = previous if ownerval is None more ownervalprevval = valval return
calcowner (! Owner!)
How you could structure the code to work
Hi Isaías Santos ,
I notice that you are using ArcMap. Sometimes using the FieldCalculator makes things a bit more complicated. You can open the Python window in ArcMap and symply edit and past the code below:
fc <SPAN class="operator token">=</SPAN> <SPAN class="string token">'Name of your feature class in TOC goes here'</SPAN> <SPAN class="comment token"># edit this!</SPAN> fld <SPAN class="operator token">=</SPAN> <SPAN class="string token">'Owner'</SPAN> <SPAN class="comment token"># name of your field to update</SPAN> prev <SPAN class="operator token">=</SPAN> None <SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>UpdateCursor<SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN>fld<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> curs<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> curs<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">==</SPAN> None<SPAN class="punctuation token">:</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> prev <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> prev <SPAN class="operator token">=</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> curs<SPAN class="punctuation token">.</SPAN>updateRow<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Notice that this code will update your data, so it will be best to test on a copy of your data.
Remember to change the name of your featuresclass on the first row as it appears in the table of content in ArcMap.
In the screenshot above you can see on the left of the screen that my featureclass is named "test", which is what I used on the first line of the code (lower center/right). My field is called "myField" (line 2 in code) and in the table you can see in field "Test" what the initial values were before running the script. Notice also that if the first record has no value, it will not be changed, since there is no initial value set. If you want to change this you can edit this on line 3 but doing something like:
prev = 'my initial value'
Isaías Santos you posted to the ModelBuilder space so I here is an example to use Xander's cursor code in the Calculate Value tool. A little bit of Python can go a long way in ModelBuilder.
<SPAN class="comment token"># Calculate Value tool</SPAN> <SPAN class="comment token"># Expression (Feature layer and Field are model variables)</SPAN> f<SPAN class="punctuation token">(</SPAN>r<SPAN class="string token">"%Feature layer%"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"%Field%"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Code Block</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">f</SPAN><SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">,</SPAN> fld<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> prev <SPAN class="operator token">=</SPAN> None <SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>UpdateCursor<SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN>fld<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> curs<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> curs<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">==</SPAN> None<SPAN class="punctuation token">:</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> prev <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> prev <SPAN class="operator token">=</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> curs<SPAN class="punctuation token">.</SPAN>updateRow<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">return</SPAN><SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Data Type</SPAN> Feature Layer<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.