Divide two or more rows in a single attribute table
Clash 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?
field-calculator attribute-table qgis-3.0 attributes fields
add a comment |Â
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?
field-calculator attribute-table qgis-3.0 attributes fields
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
add a comment |Â
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?
field-calculator attribute-table qgis-3.0 attributes fields
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?
field-calculator attribute-table qgis-3.0 attributes fields
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
add a comment |Â
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
add a comment |Â
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)
@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
 |Â
show 2 more comments
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
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.
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
 |Â
show 6 more comments
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)
@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
 |Â
show 2 more comments
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)
@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
 |Â
show 2 more comments
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)
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)
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
 |Â
show 2 more comments
@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
 |Â
show 2 more comments
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
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.
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
 |Â
show 6 more comments
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
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.
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
 |Â
show 6 more comments
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
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.
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
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.
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
 |Â
show 6 more comments
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
 |Â
show 6 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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