<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Create dynamic variable from returned value / variable in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/create-dynamic-variable-from-returned-value/m-p/1185877#M64815</link>
    <description>&lt;P&gt;You can use __getattr__() and __setattr__() built in methods on global/local namespaces to get/set variables by string name on an object or module.&lt;/P&gt;&lt;P&gt;See:&amp;nbsp;&lt;A href="https://docs.python.org/3.7/library/functions.html#getattr" target="_blank"&gt;https://docs.python.org/3.7/library/functions.html#getattr&lt;/A&gt;&amp;nbsp;and&amp;nbsp;&lt;A href="https://docs.python.org/3.7/library/functions.html#setattr" target="_blank"&gt;https://docs.python.org/3.7/library/functions.html#setattr&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 24 Jun 2022 04:40:21 GMT</pubDate>
    <dc:creator>LongDinh</dc:creator>
    <dc:date>2022-06-24T04:40:21Z</dc:date>
    <item>
      <title>Create dynamic variable from returned value / variable</title>
      <link>https://community.esri.com/t5/python-questions/create-dynamic-variable-from-returned-value/m-p/1185872#M64814</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Trying to work out how to use a returned value to make a new dynamic variable name:&lt;/P&gt;&lt;P&gt;returned value equals: 'Red' which is coming from another variable named {colour}.&lt;/P&gt;&lt;P&gt;new variable name created equals: 'Red' + '_dom' = Red_dom&lt;/P&gt;&lt;P&gt;I read about global() and local() but not sure if they are the correct approach.&lt;/P&gt;&lt;P&gt;When I try&amp;nbsp;'Red' + '_dom' I get a string error returned by system.&lt;/P&gt;&lt;P&gt;I will actually need something like {colour} + '_dom' to be the new variable name as {colour} changes within a loop.&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Craig&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jun 2022 03:46:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/create-dynamic-variable-from-returned-value/m-p/1185872#M64814</guid>
      <dc:creator>CPoynter</dc:creator>
      <dc:date>2022-06-24T03:46:04Z</dc:date>
    </item>
    <item>
      <title>Re: Create dynamic variable from returned value / variable</title>
      <link>https://community.esri.com/t5/python-questions/create-dynamic-variable-from-returned-value/m-p/1185877#M64815</link>
      <description>&lt;P&gt;You can use __getattr__() and __setattr__() built in methods on global/local namespaces to get/set variables by string name on an object or module.&lt;/P&gt;&lt;P&gt;See:&amp;nbsp;&lt;A href="https://docs.python.org/3.7/library/functions.html#getattr" target="_blank"&gt;https://docs.python.org/3.7/library/functions.html#getattr&lt;/A&gt;&amp;nbsp;and&amp;nbsp;&lt;A href="https://docs.python.org/3.7/library/functions.html#setattr" target="_blank"&gt;https://docs.python.org/3.7/library/functions.html#setattr&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jun 2022 04:40:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/create-dynamic-variable-from-returned-value/m-p/1185877#M64815</guid>
      <dc:creator>LongDinh</dc:creator>
      <dc:date>2022-06-24T04:40:21Z</dc:date>
    </item>
    <item>
      <title>Re: Create dynamic variable from returned value / variable</title>
      <link>https://community.esri.com/t5/python-questions/create-dynamic-variable-from-returned-value/m-p/1185880#M64816</link>
      <description>&lt;P&gt;That sort of hackery is not very pythonic.&amp;nbsp; What you want is called a dictionary.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;colours = {}
colour = "red"
colours[f"{colour}_dom"] = "anothervalue"

print(list(colours.keys()))

# ['red_dom']&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jun 2022 08:11:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/create-dynamic-variable-from-returned-value/m-p/1185880#M64816</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2022-06-24T08:11:08Z</dc:date>
    </item>
    <item>
      <title>Re: Create dynamic variable from returned value / variable</title>
      <link>https://community.esri.com/t5/python-questions/create-dynamic-variable-from-returned-value/m-p/1189710#M64929</link>
      <description>&lt;P&gt;If&lt;/P&gt;&lt;P&gt;print(colours[f"{colour}_dom"]) returns&lt;/P&gt;&lt;PRE&gt;anothervalue&lt;/PRE&gt;&lt;P&gt;and&lt;/P&gt;&lt;P&gt;print(colours) returns&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;{'red_dom': 'anothervalue'}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;How do return as 'red_dom' = 'anothervalue', so from my loop I can list the variable 'red_dom' etc. to equal the returned value such as:&lt;/P&gt;&lt;P&gt;'Name', 'Car', 'red_dom', 'blue_dom, 'green_dom'&lt;/P&gt;&lt;P&gt;where 'red_dom', 'blue_dom, 'green_dom' are calculated number of coloured cars for example.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jul 2022 05:26:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/create-dynamic-variable-from-returned-value/m-p/1189710#M64929</guid>
      <dc:creator>CPoynter</dc:creator>
      <dc:date>2022-07-06T05:26:05Z</dc:date>
    </item>
    <item>
      <title>Re: Create dynamic variable from returned value / variable</title>
      <link>https://community.esri.com/t5/python-questions/create-dynamic-variable-from-returned-value/m-p/1189716#M64930</link>
      <description>&lt;P&gt;Your use case is a bit unclear to me Craig, could you add a bit of pseudo-code to try and illustrate what you're trying to do?&lt;/P&gt;&lt;P&gt;But if what I think you're trying to do is correct, the following might help...?&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Just some simple dummy data,
# you could have a car class or pandas dataframe with car colour, make, model, etc... details
cars = ["red", "red", "blue", "white", "white", "red", "blue", "white"]

car_colours = {}

for colour in cars:
    car_colours[colour] = car_colours.get(colour, 0) + 1

    # could also use
    try:
        car_colours[colour] += 1
    except KeyError:
        car_colours[colour] = 1

# Now show all colours and how many cars were that colour
for colour, count in car_colours.items():
    print(f"There were {count} {colour} cars.")

# Now just print how many red and orange cars:
print(f"There were {car_colours['red']} red cars.")
print(f"There were {car_colours.get('orange', 0)} orange cars.")
# I use .get instead of ["key"] as I know there's no orange cars and would otherwise get a KeyError&lt;/LI-CODE&gt;&lt;P&gt;Output:&lt;/P&gt;&lt;LI-CODE lang="markdown"&gt;There were 6 red cars.
There were 4 blue cars.
There were 6 white cars.
There were 6 red cars.
There were 0 orange cars.&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 06 Jul 2022 06:32:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/create-dynamic-variable-from-returned-value/m-p/1189716#M64930</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2022-07-06T06:32:52Z</dc:date>
    </item>
    <item>
      <title>Re: Create dynamic variable from returned value / variable</title>
      <link>https://community.esri.com/t5/python-questions/create-dynamic-variable-from-returned-value/m-p/1190240#M64951</link>
      <description>&lt;P&gt;Hi Luke,&lt;/P&gt;&lt;P&gt;Here is the type of outcome I am after (not actual data but similar scenario):&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="CraigPoynter_0-1657169047807.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/45409i90B17DCCC252AC18/image-size/medium?v=v2&amp;amp;px=400" role="button" title="CraigPoynter_0-1657169047807.png" alt="CraigPoynter_0-1657169047807.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I have a large table of data (let's say car sales by dealer). I have done the stats to work out which vehicle type and style was sold in each colour. I know which is the maximum or dominant colour, and I am trying to capture that into a CSV document based on a ranking of colour.&lt;/P&gt;&lt;P&gt;In the script you have been addressing, I am trying to capture the dominant colour under each vehicle style so that my CSV output 'write' is something like:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;csv_data = dealer, car, sedan_dom, wagon_dom, convertible_dom&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Section of my script that I am trying to capture final value of sedan_dom, wagon_dom and convertible_dom.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;if sorted_output[0][1] == '0': (captures null/zero tuples)&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;dom = '0'&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;else:&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;most_significant_output = sorted_output[0]&lt;/STRONG&gt; (captures ranked dominant tuple)&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;STRONG&gt;dom = most_significant_output[0]&lt;/STRONG&gt; (section where I need a variable in the loop to&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; be named sedan_dom, wagon_dom, convertible_dom)&lt;/P&gt;&lt;P&gt;Hope that might clear up what I am attempting to do. Might have just made it more confusing.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jul 2022 05:01:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/create-dynamic-variable-from-returned-value/m-p/1190240#M64951</guid>
      <dc:creator>CPoynter</dc:creator>
      <dc:date>2022-07-07T05:01:42Z</dc:date>
    </item>
    <item>
      <title>Re: Create dynamic variable from returned value / variable</title>
      <link>https://community.esri.com/t5/python-questions/create-dynamic-variable-from-returned-value/m-p/1190673#M64968</link>
      <description>&lt;P&gt;You don't need "variable" variable names. Just rename the columns before you write out to CSV.&lt;/P&gt;&lt;P&gt;e.g.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;columns = ["sedan", "wagon", "convertible"] 
new_columns = [f"{c}_dom" for c in columns]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's a pandas version:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import pandas as pd
from io import StringIO  # not required if reading csv from file

# Dummy csv to demo, can also read from csv file
csv = """dealer,car,sedan,wagon,convertible, junk
A,Ford,Red,0,Green, None
A,GMC,Blue,Red,0, None
B,BMW,Green,0,Red, None
B,Ford,0,Blue,Red, None
C,BMW,0,Green,0, None
C,GMC,Red,Blue,Red, None
D,GMC,Red,Blue,Blue, None
D,GMC,Red,Blue,0, None
"""

columns = ["sedan", "wagon", "convertible"]  # ignore the junk etc, columns that aren't of interest
dom_columns = {c: f"{c}_dom" for c in columns}

df = pd.read_csv(StringIO(csv))  # or df = pd.read_csv(r"path/to/csv")
dom_df = df[columns].mode().rename(columns=dom_columns)
dom_df.to_csv("dom.csv", index=False)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jul 2022 23:11:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/create-dynamic-variable-from-returned-value/m-p/1190673#M64968</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2022-07-07T23:11:34Z</dc:date>
    </item>
  </channel>
</rss>

