# Calculating Dominant Tapestry Segment with Python

565
2
08-01-2017 11:08 AM
by Anonymous User
Not applicable

Hello all,

So i have a block groups dataset with some tapestry data appended to it. I would like to create a field that calculates the dominant tapestry group and returns a text string that references the field name of the dominant segment.

I used the max function to find the dominant segment in each block which can be seen in the "dominant" field. The next step that I can not seem to figure out is how to expand on the python script to get the dominant field to print the name of the dominant field as opposed to the number.

Any help is much appreciated!

1 Solution

Accepted Solutions
Occasional Contributor II

Looks like there is one more step missing. The max() function in Python returns the maximum value in a list, so you seem to have a list of the values. Is there a corresponding list of segments? If so, you can get the index of the maximum value using:

``````segments = ['a', 'b', 'c', 'd', 'e']
l = [1,2,3,2,1]
i = l.index(max(l))
# 2
segments[i]
# 'c'‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍``````
2 Replies
Occasional Contributor II

Looks like there is one more step missing. The max() function in Python returns the maximum value in a list, so you seem to have a list of the values. Is there a corresponding list of segments? If so, you can get the index of the maximum value using:

``````segments = ['a', 'b', 'c', 'd', 'e']
l = [1,2,3,2,1]
i = l.index(max(l))
# 2
segments[i]
# 'c'‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍``````
by Anonymous User
Not applicable

very helpful! that worked thank you.