<?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: Help extracting value from dict in a column in Data Engineering</title>
    <link>https://community.fabric.microsoft.com/t5/Data-Engineering/Help-extracting-value-from-dict-in-a-column/m-p/4255731#M4768</link>
    <description>&lt;P&gt;Hi &lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/828704"&gt;@silly_bird&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It looks like you have found a solution. Could you please mark this helpful post as “Answered”?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This will help others in the community to easily find a solution if they are experiencing the same problem as you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your cooperation!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best Regards,&lt;BR /&gt;Yang&lt;BR /&gt;Community Support Team&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If there is any post&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;helps&lt;/EM&gt;&lt;/STRONG&gt;, then please consider&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;Accept it as the solution&lt;/EM&gt;&lt;/STRONG&gt;&amp;nbsp;&amp;nbsp;to help the other members find it more quickly.&lt;BR /&gt;If I misunderstand your needs or you still have problems on it, please feel free to let us know.&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;Thanks a lot!&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 24 Oct 2024 07:09:02 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2024-10-24T07:09:02Z</dc:date>
    <item>
      <title>Help extracting value from dict in a column</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/Help-extracting-value-from-dict-in-a-column/m-p/4255381#M4752</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi all&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm working on API integration in PySpark notebook and there is a column with email &amp;amp; phone that is an array with random order&lt;/P&gt;&lt;P&gt;&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;contactMethods = [{'name': 'Email', 'value': 'email.com'}, {'name': 'Mobile', 'value': '1234'}]

df = spark.createDataFrame(
[(1, contactMethods)],
("key", "contactMethods")
)
display(df)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to translate it into "email" and "mobile" columns but can't figure out how to do this.&lt;/P&gt;&lt;P&gt;I have tried different samples of similar cases but they error in the notebook.&lt;/P&gt;&lt;P&gt;Even pyspark documentation samples don't work, for example "filter" sample: pyspark.sql.functions.filter — PySpark 3.5.3 documentation&lt;/P&gt;&lt;P&gt;I'm stuck ;(&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please help!&lt;/P&gt;</description>
      <pubDate>Thu, 24 Oct 2024 03:05:20 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/Help-extracting-value-from-dict-in-a-column/m-p/4255381#M4752</guid>
      <dc:creator>silly_bird</dc:creator>
      <dc:date>2024-10-24T03:05:20Z</dc:date>
    </item>
    <item>
      <title>Re: Help extracting value from dict in a column</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/Help-extracting-value-from-dict-in-a-column/m-p/4255420#M4754</link>
      <description>&lt;P&gt;Also to add, contact methods llist can contain from 0 to many methods, I'm interested only in "email" and "phone"&lt;/P&gt;</description>
      <pubDate>Thu, 24 Oct 2024 03:58:52 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/Help-extracting-value-from-dict-in-a-column/m-p/4255420#M4754</guid>
      <dc:creator>silly_bird</dc:creator>
      <dc:date>2024-10-24T03:58:52Z</dc:date>
    </item>
    <item>
      <title>Re: Help extracting value from dict in a column</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/Help-extracting-value-from-dict-in-a-column/m-p/4255447#M4755</link>
      <description>&lt;P&gt;It looks like I managed to figure one solution.&lt;/P&gt;&lt;P&gt;Don't know good or bad, it is my only one&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from pyspark.sql.functions import col, udf
from pyspark.sql.types import StringType

extract_email = udf(lambda cell: str(next(filter(lambda t: t["name"] == "Email", cell), {}).get("value", "")), StringType())
extract_mobile = udf(lambda cell: str(next(filter(lambda t: t["name"] == "Mobile", cell), {}).get("value", "")), StringType())
df = df.withColumn('email', extract_email(col("contactMethods"))).withColumn('mobile', extract_mobile(col("contactMethods")))
display(df)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Profies, please advise!&lt;/P&gt;</description>
      <pubDate>Thu, 24 Oct 2024 04:36:40 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/Help-extracting-value-from-dict-in-a-column/m-p/4255447#M4755</guid>
      <dc:creator>silly_bird</dc:creator>
      <dc:date>2024-10-24T04:36:40Z</dc:date>
    </item>
    <item>
      <title>Re: Help extracting value from dict in a column</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/Help-extracting-value-from-dict-in-a-column/m-p/4255458#M4756</link>
      <description>&lt;P&gt;Update&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If we read data from json, like that&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;df = spark.read.json(spark.sparkContext.parallelize([response.json()])).head(1)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;.. then cell is a an array of Row objects, not an array of dict&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I managed to workaround using asDict method on the row&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;extract_email = udf(lambda cell: None if cell is None else next(filter(lambda t: t["name"] == "Email", cell), Row(value=None)).asDict().get("value", None), StringType())&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Oct 2024 22:50:20 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/Help-extracting-value-from-dict-in-a-column/m-p/4255458#M4756</guid>
      <dc:creator>silly_bird</dc:creator>
      <dc:date>2024-10-24T22:50:20Z</dc:date>
    </item>
    <item>
      <title>Re: Help extracting value from dict in a column</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/Help-extracting-value-from-dict-in-a-column/m-p/4255731#M4768</link>
      <description>&lt;P&gt;Hi &lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/828704"&gt;@silly_bird&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It looks like you have found a solution. Could you please mark this helpful post as “Answered”?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This will help others in the community to easily find a solution if they are experiencing the same problem as you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your cooperation!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best Regards,&lt;BR /&gt;Yang&lt;BR /&gt;Community Support Team&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If there is any post&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;helps&lt;/EM&gt;&lt;/STRONG&gt;, then please consider&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;Accept it as the solution&lt;/EM&gt;&lt;/STRONG&gt;&amp;nbsp;&amp;nbsp;to help the other members find it more quickly.&lt;BR /&gt;If I misunderstand your needs or you still have problems on it, please feel free to let us know.&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;Thanks a lot!&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Oct 2024 07:09:02 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/Help-extracting-value-from-dict-in-a-column/m-p/4255731#M4768</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2024-10-24T07:09:02Z</dc:date>
    </item>
  </channel>
</rss>

