Transferring Data From One Table to Another Using Multiple Keys in ArcPy?Update value in one table (feature...
Fully submerged water bath for stove top baking?
Journal standards vs. personal standards
Why did the Apple //e make a hideous noise if you inserted the disk upside down?
How did they film the Invisible Man being invisible, in 1933?
How can I deal with extreme temperatures in a hotel room?
Do the 26 richest billionaires own as much wealth as the poorest 3.8 billion people?
How useful would a hydroelectric plant be in the post-apocalypse world?
Alien life interbreeding with Earth life
if a USA citizen marries a foreign citizen who has kid from previous marriage
Checkmate in 1 on a Tangled Board
Why was Pan Am Flight 103 flying over Lockerbie?
How do I ask a good question about a topic I am not completely familiar with?
Why can't you move another user's directory when you can move their file?
How can I know if a PDF file was created via LaTeX or XeLaTeX?
How to securely dispose of a smartphone?
Missing root certificates on Windows Server 2016 (fresh install)
Making a wall made from glass bricks
I just started should I accept a farewell lunch for a coworker I don't know?
List Manipulation : {{{a,b},{c,d}},{{e,f},{g,h}}} into {{a,b},{c,d},{e,f},{g,h}}
What is the Japanese name for the conventional shoelace knot?
Why was p[:] designed to work differently in these two situations?
Have any large aeroplanes been landed — safely and without damage — in locations that they could not be flown away from?
13th chords on guitar
Why wasn't ASCII designed with a contiguous alphanumeric character order?
Transferring Data From One Table to Another Using Multiple Keys in ArcPy?
Update value in one table (feature class) by coping it from another table. The match between table is done on values from another fieldUpdating a table with the changes made from another tableWriting Data Driven Pages' scale into driving layer's attribute table with arcpyUsing a dictionary to update one attribute based on anotherSearch Cursor Return Field Name If All Rows Contain a ValueComparing value with value from the next rowSwitching from Nested Search Cursors to DictionariesArcPy updating fields from another tableIteratively Filling a Dictionary and Updating a Feature ClassSumming Values for Each Item in a Dictionary
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I have data in one field and I want to transfer it to a specific field in another table based on specific keys Such as ID, road_type and veg_type.
My original table looks like this
ID Road_Type veg_type DENSITY
1 1 A 5.26
2 2 B 2.12
3 2 A .564
3 1 C 1.23
4 3 B .5
5 2 B 1.5
The Transfer table will have each possible combination of Road_Type and veg_type as a field and I want the density value to be placed under the field based on the ID, Road and veg keys.
ID R1_VA R2_VA R3_VA R1_VB R2_VB R3_VB R1_VC R2_VC R3_VC
1 5.26 0 0 0 0 0 0 0 0
2 0 0 0 0 2.12 0 0 0 0
3 0 .564 0 0 0 0 1.23 0 0
4 0 0 0 0 0 .5 0 0 0
5 0 0 0 0 1.5 0 0 0 0
My code just gives me zeros
Here's what it looks like:
for fc in arcpy.ListFeatureClasses('*M'):
table1 = fc + 'table'
flds = ['ID', 'Road_Type', 'veg_type', 'DENSITY']
search_fc = {f[0:2]:f[3] for f in arcpy.da.SearchCursor(table1, flds)}
uc_flds = ['ID', 'R1_VA', 'R2_VA', 'R3_VA', 'R1_VB', 'R2_VB', 'R3_VB',
'R1_VC', 'R2_VC', 'R3_VC']
with arcpy.da.UpdateCursor(fc, uc_flds) as Cursor:
for row in Cursor:
ID = row[0]
if search_fc.get(row[0]) == ID and search_fc[1] == '1' and search_fc[2] == 'A':
row[1] = search_fc[row[0]]
elif search_fc.get(row[0]) == ID and search_fc[1] == '2' and search_fc[2] == 'A':
row[2] = search_fc[row[0]]
#continue with elif statments for each field
Cursor.updateRow(row)
print str(row) +str(fc)
Is there anyway to fix it or is there a more efficient solution?
arcpy attribute-table cursor update dictionary
add a comment |
I have data in one field and I want to transfer it to a specific field in another table based on specific keys Such as ID, road_type and veg_type.
My original table looks like this
ID Road_Type veg_type DENSITY
1 1 A 5.26
2 2 B 2.12
3 2 A .564
3 1 C 1.23
4 3 B .5
5 2 B 1.5
The Transfer table will have each possible combination of Road_Type and veg_type as a field and I want the density value to be placed under the field based on the ID, Road and veg keys.
ID R1_VA R2_VA R3_VA R1_VB R2_VB R3_VB R1_VC R2_VC R3_VC
1 5.26 0 0 0 0 0 0 0 0
2 0 0 0 0 2.12 0 0 0 0
3 0 .564 0 0 0 0 1.23 0 0
4 0 0 0 0 0 .5 0 0 0
5 0 0 0 0 1.5 0 0 0 0
My code just gives me zeros
Here's what it looks like:
for fc in arcpy.ListFeatureClasses('*M'):
table1 = fc + 'table'
flds = ['ID', 'Road_Type', 'veg_type', 'DENSITY']
search_fc = {f[0:2]:f[3] for f in arcpy.da.SearchCursor(table1, flds)}
uc_flds = ['ID', 'R1_VA', 'R2_VA', 'R3_VA', 'R1_VB', 'R2_VB', 'R3_VB',
'R1_VC', 'R2_VC', 'R3_VC']
with arcpy.da.UpdateCursor(fc, uc_flds) as Cursor:
for row in Cursor:
ID = row[0]
if search_fc.get(row[0]) == ID and search_fc[1] == '1' and search_fc[2] == 'A':
row[1] = search_fc[row[0]]
elif search_fc.get(row[0]) == ID and search_fc[1] == '2' and search_fc[2] == 'A':
row[2] = search_fc[row[0]]
#continue with elif statments for each field
Cursor.updateRow(row)
print str(row) +str(fc)
Is there anyway to fix it or is there a more efficient solution?
arcpy attribute-table cursor update dictionary
Which ArcMap version do you have?
– BERA
6 hours ago
@BERA I have 10.5
– harry.p
6 hours ago
add a comment |
I have data in one field and I want to transfer it to a specific field in another table based on specific keys Such as ID, road_type and veg_type.
My original table looks like this
ID Road_Type veg_type DENSITY
1 1 A 5.26
2 2 B 2.12
3 2 A .564
3 1 C 1.23
4 3 B .5
5 2 B 1.5
The Transfer table will have each possible combination of Road_Type and veg_type as a field and I want the density value to be placed under the field based on the ID, Road and veg keys.
ID R1_VA R2_VA R3_VA R1_VB R2_VB R3_VB R1_VC R2_VC R3_VC
1 5.26 0 0 0 0 0 0 0 0
2 0 0 0 0 2.12 0 0 0 0
3 0 .564 0 0 0 0 1.23 0 0
4 0 0 0 0 0 .5 0 0 0
5 0 0 0 0 1.5 0 0 0 0
My code just gives me zeros
Here's what it looks like:
for fc in arcpy.ListFeatureClasses('*M'):
table1 = fc + 'table'
flds = ['ID', 'Road_Type', 'veg_type', 'DENSITY']
search_fc = {f[0:2]:f[3] for f in arcpy.da.SearchCursor(table1, flds)}
uc_flds = ['ID', 'R1_VA', 'R2_VA', 'R3_VA', 'R1_VB', 'R2_VB', 'R3_VB',
'R1_VC', 'R2_VC', 'R3_VC']
with arcpy.da.UpdateCursor(fc, uc_flds) as Cursor:
for row in Cursor:
ID = row[0]
if search_fc.get(row[0]) == ID and search_fc[1] == '1' and search_fc[2] == 'A':
row[1] = search_fc[row[0]]
elif search_fc.get(row[0]) == ID and search_fc[1] == '2' and search_fc[2] == 'A':
row[2] = search_fc[row[0]]
#continue with elif statments for each field
Cursor.updateRow(row)
print str(row) +str(fc)
Is there anyway to fix it or is there a more efficient solution?
arcpy attribute-table cursor update dictionary
I have data in one field and I want to transfer it to a specific field in another table based on specific keys Such as ID, road_type and veg_type.
My original table looks like this
ID Road_Type veg_type DENSITY
1 1 A 5.26
2 2 B 2.12
3 2 A .564
3 1 C 1.23
4 3 B .5
5 2 B 1.5
The Transfer table will have each possible combination of Road_Type and veg_type as a field and I want the density value to be placed under the field based on the ID, Road and veg keys.
ID R1_VA R2_VA R3_VA R1_VB R2_VB R3_VB R1_VC R2_VC R3_VC
1 5.26 0 0 0 0 0 0 0 0
2 0 0 0 0 2.12 0 0 0 0
3 0 .564 0 0 0 0 1.23 0 0
4 0 0 0 0 0 .5 0 0 0
5 0 0 0 0 1.5 0 0 0 0
My code just gives me zeros
Here's what it looks like:
for fc in arcpy.ListFeatureClasses('*M'):
table1 = fc + 'table'
flds = ['ID', 'Road_Type', 'veg_type', 'DENSITY']
search_fc = {f[0:2]:f[3] for f in arcpy.da.SearchCursor(table1, flds)}
uc_flds = ['ID', 'R1_VA', 'R2_VA', 'R3_VA', 'R1_VB', 'R2_VB', 'R3_VB',
'R1_VC', 'R2_VC', 'R3_VC']
with arcpy.da.UpdateCursor(fc, uc_flds) as Cursor:
for row in Cursor:
ID = row[0]
if search_fc.get(row[0]) == ID and search_fc[1] == '1' and search_fc[2] == 'A':
row[1] = search_fc[row[0]]
elif search_fc.get(row[0]) == ID and search_fc[1] == '2' and search_fc[2] == 'A':
row[2] = search_fc[row[0]]
#continue with elif statments for each field
Cursor.updateRow(row)
print str(row) +str(fc)
Is there anyway to fix it or is there a more efficient solution?
arcpy attribute-table cursor update dictionary
arcpy attribute-table cursor update dictionary
edited 5 hours ago
harry.p
asked 8 hours ago
harry.pharry.p
385 bronze badges
385 bronze badges
Which ArcMap version do you have?
– BERA
6 hours ago
@BERA I have 10.5
– harry.p
6 hours ago
add a comment |
Which ArcMap version do you have?
– BERA
6 hours ago
@BERA I have 10.5
– harry.p
6 hours ago
Which ArcMap version do you have?
– BERA
6 hours ago
Which ArcMap version do you have?
– BERA
6 hours ago
@BERA I have 10.5
– harry.p
6 hours ago
@BERA I have 10.5
– harry.p
6 hours ago
add a comment |
2 Answers
2
active
oldest
votes
By converting it to string I was able to get it to recognize the ID.
Theres probably a much better way but this is what I got.
for fc in arcpy.ListFeatureClasses('*M'):
table1 = fc + 'table'
flds = ['ID', 'Road_Type', 'veg_type', 'DENSITY']
searchDict = {str(f[0])+','+str(f[1])+','+str(f[2]):(f[3:]) for f in
arcpy.da.SearchCursor(table1, flds)}
uc_flds = ['ID', 'R1_VA', 'R2_VA', 'R3_VA', 'R1_VB', 'R2_VB', 'R3_VB', 'R1_VC', 'R2_VC', 'R3_VC']
print str(table1)
with arcpy.da.UpdateCursor(fc, uc_flds) as Cursor:
for row in Cursor:
ID1 = row[0]+','+ '1' +','+ 'A'
ID2 = row[0]+','+ '2' +','+ 'B'
ID3 = row[0]+','+ '3' +','+ 'C'
ID4 = row[0]+','+ '1' +','+ 'B'
ID5 = row[0]+','+ '2' +','+ 'C'
ID6 = row[0]+','+ '3' +','+ 'A'
ID7 = row[0]+','+ '1' +','+ 'C'
ID8 = row[0]+','+ '2' +','+ 'A'
ID9 = row[0]+','+ '3' +','+ 'B'
if ID1 in searchDict:
row[1] = searchDict[ID1][0]
else: row[1] =0
if ID2 in searchDict:
row[2] = searchDict[ID2][0]
else: row[2] =0
if ID3 in searchDict:
row[3] = searchDict[ID3][0]
else: row[3] =0
if ID4 in searchDict:
row[4] = searchDict[ID4][0]
else: row[4] =0
if ID5 in searchDict:
row[5] = searchDict[ID5][0]
else: row[5] =0
if ID6 in searchDict:
row[6] = searchDict[ID6][0]
else: row[6] =0
if ID7 in searchDict:
row[7] = searchDict[ID7][0]
else: row[7] =0
if ID8 in searchDict:
row[8] = searchDict[ID8][0]
else: row[8] =0
if ID9 in searchDict:
row[9] = searchDict[ID9][0]
else: row[9] =0
Cursor.updateRow(row)
print str(row) +str(fc)
add a comment |
You can also use pandas module which is included in 10.5:
import pandas as pd
import arcpy
fc = r'C:data.gdbtable' #Change
fields = ['ID', 'Road_Type', 'veg_type', 'DENSITY']
df = pd.DataFrame.from_records(data=arcpy.da.SearchCursor(fc,fields), columns=fields) #Create dataframe using da.SearchCursor
df2 = pd.pivot_table(data=df, values='DENSITY', index='ID', columns=['Road_Type','veg_type'], fill_value=0) #Pivot
df2 = df2.stack().unstack().fillna(0) #Add all possible columns even though value is missing, for example 'R1_B'
df2.columns = ['R'+'_'.join(map(str, col)) for col in df2.columns] #Multilevel to one level and add R to beginning
df2 is now:
R1_A R1_B R1_C R2_A R2_B R2_C R3_A R3_B R3_C
ID
1 5.26 0.0 0.00 0.000 0.00 0.0 0.0 0.0 0.0
2 0.00 0.0 0.00 0.000 2.12 0.0 0.0 0.0 0.0
3 0.00 0.0 1.23 0.564 0.00 0.0 0.0 0.0 0.0
4 0.00 0.0 0.00 0.000 0.00 0.0 0.0 0.5 0.0
5 0.00 0.0 0.00 0.000 1.50 0.0 0.0 0.0 0.0
Then you can do df2.to_csv(r'C:outputdata.csv') and Table To Table to convert to the format you want
add a comment |
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f327130%2ftransferring-data-from-one-table-to-another-using-multiple-keys-in-arcpy%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
By converting it to string I was able to get it to recognize the ID.
Theres probably a much better way but this is what I got.
for fc in arcpy.ListFeatureClasses('*M'):
table1 = fc + 'table'
flds = ['ID', 'Road_Type', 'veg_type', 'DENSITY']
searchDict = {str(f[0])+','+str(f[1])+','+str(f[2]):(f[3:]) for f in
arcpy.da.SearchCursor(table1, flds)}
uc_flds = ['ID', 'R1_VA', 'R2_VA', 'R3_VA', 'R1_VB', 'R2_VB', 'R3_VB', 'R1_VC', 'R2_VC', 'R3_VC']
print str(table1)
with arcpy.da.UpdateCursor(fc, uc_flds) as Cursor:
for row in Cursor:
ID1 = row[0]+','+ '1' +','+ 'A'
ID2 = row[0]+','+ '2' +','+ 'B'
ID3 = row[0]+','+ '3' +','+ 'C'
ID4 = row[0]+','+ '1' +','+ 'B'
ID5 = row[0]+','+ '2' +','+ 'C'
ID6 = row[0]+','+ '3' +','+ 'A'
ID7 = row[0]+','+ '1' +','+ 'C'
ID8 = row[0]+','+ '2' +','+ 'A'
ID9 = row[0]+','+ '3' +','+ 'B'
if ID1 in searchDict:
row[1] = searchDict[ID1][0]
else: row[1] =0
if ID2 in searchDict:
row[2] = searchDict[ID2][0]
else: row[2] =0
if ID3 in searchDict:
row[3] = searchDict[ID3][0]
else: row[3] =0
if ID4 in searchDict:
row[4] = searchDict[ID4][0]
else: row[4] =0
if ID5 in searchDict:
row[5] = searchDict[ID5][0]
else: row[5] =0
if ID6 in searchDict:
row[6] = searchDict[ID6][0]
else: row[6] =0
if ID7 in searchDict:
row[7] = searchDict[ID7][0]
else: row[7] =0
if ID8 in searchDict:
row[8] = searchDict[ID8][0]
else: row[8] =0
if ID9 in searchDict:
row[9] = searchDict[ID9][0]
else: row[9] =0
Cursor.updateRow(row)
print str(row) +str(fc)
add a comment |
By converting it to string I was able to get it to recognize the ID.
Theres probably a much better way but this is what I got.
for fc in arcpy.ListFeatureClasses('*M'):
table1 = fc + 'table'
flds = ['ID', 'Road_Type', 'veg_type', 'DENSITY']
searchDict = {str(f[0])+','+str(f[1])+','+str(f[2]):(f[3:]) for f in
arcpy.da.SearchCursor(table1, flds)}
uc_flds = ['ID', 'R1_VA', 'R2_VA', 'R3_VA', 'R1_VB', 'R2_VB', 'R3_VB', 'R1_VC', 'R2_VC', 'R3_VC']
print str(table1)
with arcpy.da.UpdateCursor(fc, uc_flds) as Cursor:
for row in Cursor:
ID1 = row[0]+','+ '1' +','+ 'A'
ID2 = row[0]+','+ '2' +','+ 'B'
ID3 = row[0]+','+ '3' +','+ 'C'
ID4 = row[0]+','+ '1' +','+ 'B'
ID5 = row[0]+','+ '2' +','+ 'C'
ID6 = row[0]+','+ '3' +','+ 'A'
ID7 = row[0]+','+ '1' +','+ 'C'
ID8 = row[0]+','+ '2' +','+ 'A'
ID9 = row[0]+','+ '3' +','+ 'B'
if ID1 in searchDict:
row[1] = searchDict[ID1][0]
else: row[1] =0
if ID2 in searchDict:
row[2] = searchDict[ID2][0]
else: row[2] =0
if ID3 in searchDict:
row[3] = searchDict[ID3][0]
else: row[3] =0
if ID4 in searchDict:
row[4] = searchDict[ID4][0]
else: row[4] =0
if ID5 in searchDict:
row[5] = searchDict[ID5][0]
else: row[5] =0
if ID6 in searchDict:
row[6] = searchDict[ID6][0]
else: row[6] =0
if ID7 in searchDict:
row[7] = searchDict[ID7][0]
else: row[7] =0
if ID8 in searchDict:
row[8] = searchDict[ID8][0]
else: row[8] =0
if ID9 in searchDict:
row[9] = searchDict[ID9][0]
else: row[9] =0
Cursor.updateRow(row)
print str(row) +str(fc)
add a comment |
By converting it to string I was able to get it to recognize the ID.
Theres probably a much better way but this is what I got.
for fc in arcpy.ListFeatureClasses('*M'):
table1 = fc + 'table'
flds = ['ID', 'Road_Type', 'veg_type', 'DENSITY']
searchDict = {str(f[0])+','+str(f[1])+','+str(f[2]):(f[3:]) for f in
arcpy.da.SearchCursor(table1, flds)}
uc_flds = ['ID', 'R1_VA', 'R2_VA', 'R3_VA', 'R1_VB', 'R2_VB', 'R3_VB', 'R1_VC', 'R2_VC', 'R3_VC']
print str(table1)
with arcpy.da.UpdateCursor(fc, uc_flds) as Cursor:
for row in Cursor:
ID1 = row[0]+','+ '1' +','+ 'A'
ID2 = row[0]+','+ '2' +','+ 'B'
ID3 = row[0]+','+ '3' +','+ 'C'
ID4 = row[0]+','+ '1' +','+ 'B'
ID5 = row[0]+','+ '2' +','+ 'C'
ID6 = row[0]+','+ '3' +','+ 'A'
ID7 = row[0]+','+ '1' +','+ 'C'
ID8 = row[0]+','+ '2' +','+ 'A'
ID9 = row[0]+','+ '3' +','+ 'B'
if ID1 in searchDict:
row[1] = searchDict[ID1][0]
else: row[1] =0
if ID2 in searchDict:
row[2] = searchDict[ID2][0]
else: row[2] =0
if ID3 in searchDict:
row[3] = searchDict[ID3][0]
else: row[3] =0
if ID4 in searchDict:
row[4] = searchDict[ID4][0]
else: row[4] =0
if ID5 in searchDict:
row[5] = searchDict[ID5][0]
else: row[5] =0
if ID6 in searchDict:
row[6] = searchDict[ID6][0]
else: row[6] =0
if ID7 in searchDict:
row[7] = searchDict[ID7][0]
else: row[7] =0
if ID8 in searchDict:
row[8] = searchDict[ID8][0]
else: row[8] =0
if ID9 in searchDict:
row[9] = searchDict[ID9][0]
else: row[9] =0
Cursor.updateRow(row)
print str(row) +str(fc)
By converting it to string I was able to get it to recognize the ID.
Theres probably a much better way but this is what I got.
for fc in arcpy.ListFeatureClasses('*M'):
table1 = fc + 'table'
flds = ['ID', 'Road_Type', 'veg_type', 'DENSITY']
searchDict = {str(f[0])+','+str(f[1])+','+str(f[2]):(f[3:]) for f in
arcpy.da.SearchCursor(table1, flds)}
uc_flds = ['ID', 'R1_VA', 'R2_VA', 'R3_VA', 'R1_VB', 'R2_VB', 'R3_VB', 'R1_VC', 'R2_VC', 'R3_VC']
print str(table1)
with arcpy.da.UpdateCursor(fc, uc_flds) as Cursor:
for row in Cursor:
ID1 = row[0]+','+ '1' +','+ 'A'
ID2 = row[0]+','+ '2' +','+ 'B'
ID3 = row[0]+','+ '3' +','+ 'C'
ID4 = row[0]+','+ '1' +','+ 'B'
ID5 = row[0]+','+ '2' +','+ 'C'
ID6 = row[0]+','+ '3' +','+ 'A'
ID7 = row[0]+','+ '1' +','+ 'C'
ID8 = row[0]+','+ '2' +','+ 'A'
ID9 = row[0]+','+ '3' +','+ 'B'
if ID1 in searchDict:
row[1] = searchDict[ID1][0]
else: row[1] =0
if ID2 in searchDict:
row[2] = searchDict[ID2][0]
else: row[2] =0
if ID3 in searchDict:
row[3] = searchDict[ID3][0]
else: row[3] =0
if ID4 in searchDict:
row[4] = searchDict[ID4][0]
else: row[4] =0
if ID5 in searchDict:
row[5] = searchDict[ID5][0]
else: row[5] =0
if ID6 in searchDict:
row[6] = searchDict[ID6][0]
else: row[6] =0
if ID7 in searchDict:
row[7] = searchDict[ID7][0]
else: row[7] =0
if ID8 in searchDict:
row[8] = searchDict[ID8][0]
else: row[8] =0
if ID9 in searchDict:
row[9] = searchDict[ID9][0]
else: row[9] =0
Cursor.updateRow(row)
print str(row) +str(fc)
answered 5 hours ago
harry.pharry.p
385 bronze badges
385 bronze badges
add a comment |
add a comment |
You can also use pandas module which is included in 10.5:
import pandas as pd
import arcpy
fc = r'C:data.gdbtable' #Change
fields = ['ID', 'Road_Type', 'veg_type', 'DENSITY']
df = pd.DataFrame.from_records(data=arcpy.da.SearchCursor(fc,fields), columns=fields) #Create dataframe using da.SearchCursor
df2 = pd.pivot_table(data=df, values='DENSITY', index='ID', columns=['Road_Type','veg_type'], fill_value=0) #Pivot
df2 = df2.stack().unstack().fillna(0) #Add all possible columns even though value is missing, for example 'R1_B'
df2.columns = ['R'+'_'.join(map(str, col)) for col in df2.columns] #Multilevel to one level and add R to beginning
df2 is now:
R1_A R1_B R1_C R2_A R2_B R2_C R3_A R3_B R3_C
ID
1 5.26 0.0 0.00 0.000 0.00 0.0 0.0 0.0 0.0
2 0.00 0.0 0.00 0.000 2.12 0.0 0.0 0.0 0.0
3 0.00 0.0 1.23 0.564 0.00 0.0 0.0 0.0 0.0
4 0.00 0.0 0.00 0.000 0.00 0.0 0.0 0.5 0.0
5 0.00 0.0 0.00 0.000 1.50 0.0 0.0 0.0 0.0
Then you can do df2.to_csv(r'C:outputdata.csv') and Table To Table to convert to the format you want
add a comment |
You can also use pandas module which is included in 10.5:
import pandas as pd
import arcpy
fc = r'C:data.gdbtable' #Change
fields = ['ID', 'Road_Type', 'veg_type', 'DENSITY']
df = pd.DataFrame.from_records(data=arcpy.da.SearchCursor(fc,fields), columns=fields) #Create dataframe using da.SearchCursor
df2 = pd.pivot_table(data=df, values='DENSITY', index='ID', columns=['Road_Type','veg_type'], fill_value=0) #Pivot
df2 = df2.stack().unstack().fillna(0) #Add all possible columns even though value is missing, for example 'R1_B'
df2.columns = ['R'+'_'.join(map(str, col)) for col in df2.columns] #Multilevel to one level and add R to beginning
df2 is now:
R1_A R1_B R1_C R2_A R2_B R2_C R3_A R3_B R3_C
ID
1 5.26 0.0 0.00 0.000 0.00 0.0 0.0 0.0 0.0
2 0.00 0.0 0.00 0.000 2.12 0.0 0.0 0.0 0.0
3 0.00 0.0 1.23 0.564 0.00 0.0 0.0 0.0 0.0
4 0.00 0.0 0.00 0.000 0.00 0.0 0.0 0.5 0.0
5 0.00 0.0 0.00 0.000 1.50 0.0 0.0 0.0 0.0
Then you can do df2.to_csv(r'C:outputdata.csv') and Table To Table to convert to the format you want
add a comment |
You can also use pandas module which is included in 10.5:
import pandas as pd
import arcpy
fc = r'C:data.gdbtable' #Change
fields = ['ID', 'Road_Type', 'veg_type', 'DENSITY']
df = pd.DataFrame.from_records(data=arcpy.da.SearchCursor(fc,fields), columns=fields) #Create dataframe using da.SearchCursor
df2 = pd.pivot_table(data=df, values='DENSITY', index='ID', columns=['Road_Type','veg_type'], fill_value=0) #Pivot
df2 = df2.stack().unstack().fillna(0) #Add all possible columns even though value is missing, for example 'R1_B'
df2.columns = ['R'+'_'.join(map(str, col)) for col in df2.columns] #Multilevel to one level and add R to beginning
df2 is now:
R1_A R1_B R1_C R2_A R2_B R2_C R3_A R3_B R3_C
ID
1 5.26 0.0 0.00 0.000 0.00 0.0 0.0 0.0 0.0
2 0.00 0.0 0.00 0.000 2.12 0.0 0.0 0.0 0.0
3 0.00 0.0 1.23 0.564 0.00 0.0 0.0 0.0 0.0
4 0.00 0.0 0.00 0.000 0.00 0.0 0.0 0.5 0.0
5 0.00 0.0 0.00 0.000 1.50 0.0 0.0 0.0 0.0
Then you can do df2.to_csv(r'C:outputdata.csv') and Table To Table to convert to the format you want
You can also use pandas module which is included in 10.5:
import pandas as pd
import arcpy
fc = r'C:data.gdbtable' #Change
fields = ['ID', 'Road_Type', 'veg_type', 'DENSITY']
df = pd.DataFrame.from_records(data=arcpy.da.SearchCursor(fc,fields), columns=fields) #Create dataframe using da.SearchCursor
df2 = pd.pivot_table(data=df, values='DENSITY', index='ID', columns=['Road_Type','veg_type'], fill_value=0) #Pivot
df2 = df2.stack().unstack().fillna(0) #Add all possible columns even though value is missing, for example 'R1_B'
df2.columns = ['R'+'_'.join(map(str, col)) for col in df2.columns] #Multilevel to one level and add R to beginning
df2 is now:
R1_A R1_B R1_C R2_A R2_B R2_C R3_A R3_B R3_C
ID
1 5.26 0.0 0.00 0.000 0.00 0.0 0.0 0.0 0.0
2 0.00 0.0 0.00 0.000 2.12 0.0 0.0 0.0 0.0
3 0.00 0.0 1.23 0.564 0.00 0.0 0.0 0.0 0.0
4 0.00 0.0 0.00 0.000 0.00 0.0 0.0 0.5 0.0
5 0.00 0.0 0.00 0.000 1.50 0.0 0.0 0.0 0.0
Then you can do df2.to_csv(r'C:outputdata.csv') and Table To Table to convert to the format you want
answered 5 hours ago
BERABERA
19.1k6 gold badges21 silver badges46 bronze badges
19.1k6 gold badges21 silver badges46 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Geographic Information Systems Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f327130%2ftransferring-data-from-one-table-to-another-using-multiple-keys-in-arcpy%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Which ArcMap version do you have?
– BERA
6 hours ago
@BERA I have 10.5
– harry.p
6 hours ago