Divide two or more rows in a single attribute table

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
5
down vote

favorite












I want to divide rows in my attribute table. I need to divide the lengths of rows having specific IDs. For instance the length of ID 21 divided by the length of ID 11. An this calculation execute for all rows in the table This can be easily solve with the field calculator. But what if I want to keep dividing all the rows (e.g. id 22/12, 23/13, 24/14, etc...)



Is there a way to automatic do this calculation for all rows?



enter image description here







share|improve this question






















  • I wan to divide the lengths of rows having specific IDs. For instance the length of ID 21 divided by the length of ID 11. An this calculation execute for all rows in the table
    – nico
    Sep 6 at 11:46










  • There is more than one way to "divide" a row -- please Edit the question to clarify your meaning by providing a complete example of inputs and outputs.
    – Vince
    Sep 6 at 11:50










  • Always 10 ID units apart?
    – BERA
    Sep 6 at 11:53










  • Yes, IDs are always 10 units apart
    – nico
    Sep 6 at 11:56
















up vote
5
down vote

favorite












I want to divide rows in my attribute table. I need to divide the lengths of rows having specific IDs. For instance the length of ID 21 divided by the length of ID 11. An this calculation execute for all rows in the table This can be easily solve with the field calculator. But what if I want to keep dividing all the rows (e.g. id 22/12, 23/13, 24/14, etc...)



Is there a way to automatic do this calculation for all rows?



enter image description here







share|improve this question






















  • I wan to divide the lengths of rows having specific IDs. For instance the length of ID 21 divided by the length of ID 11. An this calculation execute for all rows in the table
    – nico
    Sep 6 at 11:46










  • There is more than one way to "divide" a row -- please Edit the question to clarify your meaning by providing a complete example of inputs and outputs.
    – Vince
    Sep 6 at 11:50










  • Always 10 ID units apart?
    – BERA
    Sep 6 at 11:53










  • Yes, IDs are always 10 units apart
    – nico
    Sep 6 at 11:56












up vote
5
down vote

favorite









up vote
5
down vote

favorite











I want to divide rows in my attribute table. I need to divide the lengths of rows having specific IDs. For instance the length of ID 21 divided by the length of ID 11. An this calculation execute for all rows in the table This can be easily solve with the field calculator. But what if I want to keep dividing all the rows (e.g. id 22/12, 23/13, 24/14, etc...)



Is there a way to automatic do this calculation for all rows?



enter image description here







share|improve this question














I want to divide rows in my attribute table. I need to divide the lengths of rows having specific IDs. For instance the length of ID 21 divided by the length of ID 11. An this calculation execute for all rows in the table This can be easily solve with the field calculator. But what if I want to keep dividing all the rows (e.g. id 22/12, 23/13, 24/14, etc...)



Is there a way to automatic do this calculation for all rows?



enter image description here









share|improve this question













share|improve this question




share|improve this question








edited Sep 6 at 12:05

























asked Sep 6 at 11:14









nico

928




928











  • I wan to divide the lengths of rows having specific IDs. For instance the length of ID 21 divided by the length of ID 11. An this calculation execute for all rows in the table
    – nico
    Sep 6 at 11:46










  • There is more than one way to "divide" a row -- please Edit the question to clarify your meaning by providing a complete example of inputs and outputs.
    – Vince
    Sep 6 at 11:50










  • Always 10 ID units apart?
    – BERA
    Sep 6 at 11:53










  • Yes, IDs are always 10 units apart
    – nico
    Sep 6 at 11:56
















  • I wan to divide the lengths of rows having specific IDs. For instance the length of ID 21 divided by the length of ID 11. An this calculation execute for all rows in the table
    – nico
    Sep 6 at 11:46










  • There is more than one way to "divide" a row -- please Edit the question to clarify your meaning by providing a complete example of inputs and outputs.
    – Vince
    Sep 6 at 11:50










  • Always 10 ID units apart?
    – BERA
    Sep 6 at 11:53










  • Yes, IDs are always 10 units apart
    – nico
    Sep 6 at 11:56















I wan to divide the lengths of rows having specific IDs. For instance the length of ID 21 divided by the length of ID 11. An this calculation execute for all rows in the table
– nico
Sep 6 at 11:46




I wan to divide the lengths of rows having specific IDs. For instance the length of ID 21 divided by the length of ID 11. An this calculation execute for all rows in the table
– nico
Sep 6 at 11:46












There is more than one way to "divide" a row -- please Edit the question to clarify your meaning by providing a complete example of inputs and outputs.
– Vince
Sep 6 at 11:50




There is more than one way to "divide" a row -- please Edit the question to clarify your meaning by providing a complete example of inputs and outputs.
– Vince
Sep 6 at 11:50












Always 10 ID units apart?
– BERA
Sep 6 at 11:53




Always 10 ID units apart?
– BERA
Sep 6 at 11:53












Yes, IDs are always 10 units apart
– nico
Sep 6 at 11:56




Yes, IDs are always 10 units apart
– nico
Sep 6 at 11:56










2 Answers
2






active

oldest

votes

















up vote
5
down vote



accepted










You can store ID and length in a dictionary with ID as key and length as value. Then supply current ID-10 to the dictionary and get length. For some IDs there will not be a ID-10, those will get value 999 (or whatever you want).



idfield = 'ID' #Change ID to match the name of your id field
oldlengthfield = 'Shape_Leng' #Change name
newlengthfield = 'newLength' #Change name

layer = iface.activeLayer()
d = feature[idfield]:feature[oldlengthfield] for feature in layer.getFeatures() #Store IDs and lengths in a dictionary

layer = iface.activeLayer()
with edit(layer):
for feature in layer.getFeatures():
if feature[idfield]-10 in d:
new_value = feature[oldlengthfield]/d[feature[idfield]-10]
else:
new_value = 999
feature.setAttribute(feature.fieldNameIndex(newlengthfield), new_value)
layer.updateFeature(feature)


enter image description here






share|improve this answer






















  • @Kazuhito do you know how to prevent it from printing True/False for each row in the python console?
    – BERA
    Sep 6 at 12:32







  • 1




    I have to confess, I have achieved very little on python (though I vowed to learn it earlier this year ...) I am useless here. sorry :(
    – Kazuhito
    Sep 6 at 12:38










  • sorry I am a beginner in PyQGIS. For some reason I cant get those scripts to work :(. When running the code I get this error --- Traceback (most recent call last): File "C:PROGRA~1QGIS3~1.2appsPython36libcode.py", line 91, in runcode exec(code, self.locals) File "<input>", line 1, in <module> File "<string>", line 15, in <module> KeyError: '-1' ---
    – nico
    Sep 6 at 15:09






  • 1




    And you have created the new length field with data type real(?)(/float/double/decimal)? The code will not create the new field.
    – BERA
    Sep 6 at 15:24







  • 1




    It works!!!!!! I thought the script creates automatically the new field. Sorry for the inexperience. Thank you very much :)
    – nico
    Sep 6 at 15:28

















up vote
3
down vote













From your attribute table (as your posted image);



(1) start the Field Calculator



(2) Create a new field by naming a new Output field name (e.g. divided).



(3) Select Decimal number for the output field type.



(4) Enter expression as below and click on OK



enter image description here



QGIS 3



attribute(get_feature_by_id('your_layer', "id"+10), 'length') / "length"


QGIS 2



attribute(get_feature('your_layer', 'id', attribute($currentfeature, 'id')+10), 'length') / "length"


Please change 'your_layer' to your actual layer name.






share|improve this answer


















  • 1




    That was easier!
    – BERA
    Sep 6 at 12:22











  • Thanks @BERA ! :)
    – Kazuhito
    Sep 6 at 12:24










  • sorry I am a beginner in PyQGIS. For some reason I cant get those scripts to work :(
    – nico
    Sep 6 at 13:34






  • 1




    @nico They are expressions for the Field Calculator.
    – Kazuhito
    Sep 6 at 13:36










  • what is the difference between " " and ' ' ?
    – nico
    Sep 6 at 13:51










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "79"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f295130%2fdivide-two-or-more-rows-in-a-single-attribute-table%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
5
down vote



accepted










You can store ID and length in a dictionary with ID as key and length as value. Then supply current ID-10 to the dictionary and get length. For some IDs there will not be a ID-10, those will get value 999 (or whatever you want).



idfield = 'ID' #Change ID to match the name of your id field
oldlengthfield = 'Shape_Leng' #Change name
newlengthfield = 'newLength' #Change name

layer = iface.activeLayer()
d = feature[idfield]:feature[oldlengthfield] for feature in layer.getFeatures() #Store IDs and lengths in a dictionary

layer = iface.activeLayer()
with edit(layer):
for feature in layer.getFeatures():
if feature[idfield]-10 in d:
new_value = feature[oldlengthfield]/d[feature[idfield]-10]
else:
new_value = 999
feature.setAttribute(feature.fieldNameIndex(newlengthfield), new_value)
layer.updateFeature(feature)


enter image description here






share|improve this answer






















  • @Kazuhito do you know how to prevent it from printing True/False for each row in the python console?
    – BERA
    Sep 6 at 12:32







  • 1




    I have to confess, I have achieved very little on python (though I vowed to learn it earlier this year ...) I am useless here. sorry :(
    – Kazuhito
    Sep 6 at 12:38










  • sorry I am a beginner in PyQGIS. For some reason I cant get those scripts to work :(. When running the code I get this error --- Traceback (most recent call last): File "C:PROGRA~1QGIS3~1.2appsPython36libcode.py", line 91, in runcode exec(code, self.locals) File "<input>", line 1, in <module> File "<string>", line 15, in <module> KeyError: '-1' ---
    – nico
    Sep 6 at 15:09






  • 1




    And you have created the new length field with data type real(?)(/float/double/decimal)? The code will not create the new field.
    – BERA
    Sep 6 at 15:24







  • 1




    It works!!!!!! I thought the script creates automatically the new field. Sorry for the inexperience. Thank you very much :)
    – nico
    Sep 6 at 15:28














up vote
5
down vote



accepted










You can store ID and length in a dictionary with ID as key and length as value. Then supply current ID-10 to the dictionary and get length. For some IDs there will not be a ID-10, those will get value 999 (or whatever you want).



idfield = 'ID' #Change ID to match the name of your id field
oldlengthfield = 'Shape_Leng' #Change name
newlengthfield = 'newLength' #Change name

layer = iface.activeLayer()
d = feature[idfield]:feature[oldlengthfield] for feature in layer.getFeatures() #Store IDs and lengths in a dictionary

layer = iface.activeLayer()
with edit(layer):
for feature in layer.getFeatures():
if feature[idfield]-10 in d:
new_value = feature[oldlengthfield]/d[feature[idfield]-10]
else:
new_value = 999
feature.setAttribute(feature.fieldNameIndex(newlengthfield), new_value)
layer.updateFeature(feature)


enter image description here






share|improve this answer






















  • @Kazuhito do you know how to prevent it from printing True/False for each row in the python console?
    – BERA
    Sep 6 at 12:32







  • 1




    I have to confess, I have achieved very little on python (though I vowed to learn it earlier this year ...) I am useless here. sorry :(
    – Kazuhito
    Sep 6 at 12:38










  • sorry I am a beginner in PyQGIS. For some reason I cant get those scripts to work :(. When running the code I get this error --- Traceback (most recent call last): File "C:PROGRA~1QGIS3~1.2appsPython36libcode.py", line 91, in runcode exec(code, self.locals) File "<input>", line 1, in <module> File "<string>", line 15, in <module> KeyError: '-1' ---
    – nico
    Sep 6 at 15:09






  • 1




    And you have created the new length field with data type real(?)(/float/double/decimal)? The code will not create the new field.
    – BERA
    Sep 6 at 15:24







  • 1




    It works!!!!!! I thought the script creates automatically the new field. Sorry for the inexperience. Thank you very much :)
    – nico
    Sep 6 at 15:28












up vote
5
down vote



accepted







up vote
5
down vote



accepted






You can store ID and length in a dictionary with ID as key and length as value. Then supply current ID-10 to the dictionary and get length. For some IDs there will not be a ID-10, those will get value 999 (or whatever you want).



idfield = 'ID' #Change ID to match the name of your id field
oldlengthfield = 'Shape_Leng' #Change name
newlengthfield = 'newLength' #Change name

layer = iface.activeLayer()
d = feature[idfield]:feature[oldlengthfield] for feature in layer.getFeatures() #Store IDs and lengths in a dictionary

layer = iface.activeLayer()
with edit(layer):
for feature in layer.getFeatures():
if feature[idfield]-10 in d:
new_value = feature[oldlengthfield]/d[feature[idfield]-10]
else:
new_value = 999
feature.setAttribute(feature.fieldNameIndex(newlengthfield), new_value)
layer.updateFeature(feature)


enter image description here






share|improve this answer














You can store ID and length in a dictionary with ID as key and length as value. Then supply current ID-10 to the dictionary and get length. For some IDs there will not be a ID-10, those will get value 999 (or whatever you want).



idfield = 'ID' #Change ID to match the name of your id field
oldlengthfield = 'Shape_Leng' #Change name
newlengthfield = 'newLength' #Change name

layer = iface.activeLayer()
d = feature[idfield]:feature[oldlengthfield] for feature in layer.getFeatures() #Store IDs and lengths in a dictionary

layer = iface.activeLayer()
with edit(layer):
for feature in layer.getFeatures():
if feature[idfield]-10 in d:
new_value = feature[oldlengthfield]/d[feature[idfield]-10]
else:
new_value = 999
feature.setAttribute(feature.fieldNameIndex(newlengthfield), new_value)
layer.updateFeature(feature)


enter image description here







share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 7 at 11:54

























answered Sep 6 at 12:18









BERA

11.1k41435




11.1k41435











  • @Kazuhito do you know how to prevent it from printing True/False for each row in the python console?
    – BERA
    Sep 6 at 12:32







  • 1




    I have to confess, I have achieved very little on python (though I vowed to learn it earlier this year ...) I am useless here. sorry :(
    – Kazuhito
    Sep 6 at 12:38










  • sorry I am a beginner in PyQGIS. For some reason I cant get those scripts to work :(. When running the code I get this error --- Traceback (most recent call last): File "C:PROGRA~1QGIS3~1.2appsPython36libcode.py", line 91, in runcode exec(code, self.locals) File "<input>", line 1, in <module> File "<string>", line 15, in <module> KeyError: '-1' ---
    – nico
    Sep 6 at 15:09






  • 1




    And you have created the new length field with data type real(?)(/float/double/decimal)? The code will not create the new field.
    – BERA
    Sep 6 at 15:24







  • 1




    It works!!!!!! I thought the script creates automatically the new field. Sorry for the inexperience. Thank you very much :)
    – nico
    Sep 6 at 15:28
















  • @Kazuhito do you know how to prevent it from printing True/False for each row in the python console?
    – BERA
    Sep 6 at 12:32







  • 1




    I have to confess, I have achieved very little on python (though I vowed to learn it earlier this year ...) I am useless here. sorry :(
    – Kazuhito
    Sep 6 at 12:38










  • sorry I am a beginner in PyQGIS. For some reason I cant get those scripts to work :(. When running the code I get this error --- Traceback (most recent call last): File "C:PROGRA~1QGIS3~1.2appsPython36libcode.py", line 91, in runcode exec(code, self.locals) File "<input>", line 1, in <module> File "<string>", line 15, in <module> KeyError: '-1' ---
    – nico
    Sep 6 at 15:09






  • 1




    And you have created the new length field with data type real(?)(/float/double/decimal)? The code will not create the new field.
    – BERA
    Sep 6 at 15:24







  • 1




    It works!!!!!! I thought the script creates automatically the new field. Sorry for the inexperience. Thank you very much :)
    – nico
    Sep 6 at 15:28















@Kazuhito do you know how to prevent it from printing True/False for each row in the python console?
– BERA
Sep 6 at 12:32





@Kazuhito do you know how to prevent it from printing True/False for each row in the python console?
– BERA
Sep 6 at 12:32





1




1




I have to confess, I have achieved very little on python (though I vowed to learn it earlier this year ...) I am useless here. sorry :(
– Kazuhito
Sep 6 at 12:38




I have to confess, I have achieved very little on python (though I vowed to learn it earlier this year ...) I am useless here. sorry :(
– Kazuhito
Sep 6 at 12:38












sorry I am a beginner in PyQGIS. For some reason I cant get those scripts to work :(. When running the code I get this error --- Traceback (most recent call last): File "C:PROGRA~1QGIS3~1.2appsPython36libcode.py", line 91, in runcode exec(code, self.locals) File "<input>", line 1, in <module> File "<string>", line 15, in <module> KeyError: '-1' ---
– nico
Sep 6 at 15:09




sorry I am a beginner in PyQGIS. For some reason I cant get those scripts to work :(. When running the code I get this error --- Traceback (most recent call last): File "C:PROGRA~1QGIS3~1.2appsPython36libcode.py", line 91, in runcode exec(code, self.locals) File "<input>", line 1, in <module> File "<string>", line 15, in <module> KeyError: '-1' ---
– nico
Sep 6 at 15:09




1




1




And you have created the new length field with data type real(?)(/float/double/decimal)? The code will not create the new field.
– BERA
Sep 6 at 15:24





And you have created the new length field with data type real(?)(/float/double/decimal)? The code will not create the new field.
– BERA
Sep 6 at 15:24





1




1




It works!!!!!! I thought the script creates automatically the new field. Sorry for the inexperience. Thank you very much :)
– nico
Sep 6 at 15:28




It works!!!!!! I thought the script creates automatically the new field. Sorry for the inexperience. Thank you very much :)
– nico
Sep 6 at 15:28












up vote
3
down vote













From your attribute table (as your posted image);



(1) start the Field Calculator



(2) Create a new field by naming a new Output field name (e.g. divided).



(3) Select Decimal number for the output field type.



(4) Enter expression as below and click on OK



enter image description here



QGIS 3



attribute(get_feature_by_id('your_layer', "id"+10), 'length') / "length"


QGIS 2



attribute(get_feature('your_layer', 'id', attribute($currentfeature, 'id')+10), 'length') / "length"


Please change 'your_layer' to your actual layer name.






share|improve this answer


















  • 1




    That was easier!
    – BERA
    Sep 6 at 12:22











  • Thanks @BERA ! :)
    – Kazuhito
    Sep 6 at 12:24










  • sorry I am a beginner in PyQGIS. For some reason I cant get those scripts to work :(
    – nico
    Sep 6 at 13:34






  • 1




    @nico They are expressions for the Field Calculator.
    – Kazuhito
    Sep 6 at 13:36










  • what is the difference between " " and ' ' ?
    – nico
    Sep 6 at 13:51














up vote
3
down vote













From your attribute table (as your posted image);



(1) start the Field Calculator



(2) Create a new field by naming a new Output field name (e.g. divided).



(3) Select Decimal number for the output field type.



(4) Enter expression as below and click on OK



enter image description here



QGIS 3



attribute(get_feature_by_id('your_layer', "id"+10), 'length') / "length"


QGIS 2



attribute(get_feature('your_layer', 'id', attribute($currentfeature, 'id')+10), 'length') / "length"


Please change 'your_layer' to your actual layer name.






share|improve this answer


















  • 1




    That was easier!
    – BERA
    Sep 6 at 12:22











  • Thanks @BERA ! :)
    – Kazuhito
    Sep 6 at 12:24










  • sorry I am a beginner in PyQGIS. For some reason I cant get those scripts to work :(
    – nico
    Sep 6 at 13:34






  • 1




    @nico They are expressions for the Field Calculator.
    – Kazuhito
    Sep 6 at 13:36










  • what is the difference between " " and ' ' ?
    – nico
    Sep 6 at 13:51












up vote
3
down vote










up vote
3
down vote









From your attribute table (as your posted image);



(1) start the Field Calculator



(2) Create a new field by naming a new Output field name (e.g. divided).



(3) Select Decimal number for the output field type.



(4) Enter expression as below and click on OK



enter image description here



QGIS 3



attribute(get_feature_by_id('your_layer', "id"+10), 'length') / "length"


QGIS 2



attribute(get_feature('your_layer', 'id', attribute($currentfeature, 'id')+10), 'length') / "length"


Please change 'your_layer' to your actual layer name.






share|improve this answer














From your attribute table (as your posted image);



(1) start the Field Calculator



(2) Create a new field by naming a new Output field name (e.g. divided).



(3) Select Decimal number for the output field type.



(4) Enter expression as below and click on OK



enter image description here



QGIS 3



attribute(get_feature_by_id('your_layer', "id"+10), 'length') / "length"


QGIS 2



attribute(get_feature('your_layer', 'id', attribute($currentfeature, 'id')+10), 'length') / "length"


Please change 'your_layer' to your actual layer name.







share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 6 at 13:52

























answered Sep 6 at 12:21









Kazuhito

12.2k31271




12.2k31271







  • 1




    That was easier!
    – BERA
    Sep 6 at 12:22











  • Thanks @BERA ! :)
    – Kazuhito
    Sep 6 at 12:24










  • sorry I am a beginner in PyQGIS. For some reason I cant get those scripts to work :(
    – nico
    Sep 6 at 13:34






  • 1




    @nico They are expressions for the Field Calculator.
    – Kazuhito
    Sep 6 at 13:36










  • what is the difference between " " and ' ' ?
    – nico
    Sep 6 at 13:51












  • 1




    That was easier!
    – BERA
    Sep 6 at 12:22











  • Thanks @BERA ! :)
    – Kazuhito
    Sep 6 at 12:24










  • sorry I am a beginner in PyQGIS. For some reason I cant get those scripts to work :(
    – nico
    Sep 6 at 13:34






  • 1




    @nico They are expressions for the Field Calculator.
    – Kazuhito
    Sep 6 at 13:36










  • what is the difference between " " and ' ' ?
    – nico
    Sep 6 at 13:51







1




1




That was easier!
– BERA
Sep 6 at 12:22





That was easier!
– BERA
Sep 6 at 12:22













Thanks @BERA ! :)
– Kazuhito
Sep 6 at 12:24




Thanks @BERA ! :)
– Kazuhito
Sep 6 at 12:24












sorry I am a beginner in PyQGIS. For some reason I cant get those scripts to work :(
– nico
Sep 6 at 13:34




sorry I am a beginner in PyQGIS. For some reason I cant get those scripts to work :(
– nico
Sep 6 at 13:34




1




1




@nico They are expressions for the Field Calculator.
– Kazuhito
Sep 6 at 13:36




@nico They are expressions for the Field Calculator.
– Kazuhito
Sep 6 at 13:36












what is the difference between " " and ' ' ?
– nico
Sep 6 at 13:51




what is the difference between " " and ' ' ?
– nico
Sep 6 at 13:51

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f295130%2fdivide-two-or-more-rows-in-a-single-attribute-table%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

Long meetings (6-7 hours a day): Being “babysat” by supervisor

Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

Confectionery