<?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 Using map_filter / filter to return only map elements for a given predicate in Get Started Discussions</title>
    <link>https://community.databricks.com/t5/get-started-discussions/using-map-filter-filter-to-return-only-map-elements-for-a-given/m-p/48925#M5988</link>
    <description>&lt;P&gt;I want to apply a filter to a map structure (on a column called "ActivityMap") for elements only where a given predicate holds.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Representative data is below.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Applying both "map_filter" and "array_contains" will return rows where a predicate holds, &lt;U&gt;however&lt;/U&gt; the latter returns the entire row cell contents.&lt;BR /&gt;eg: all elements from rows with the key "08_S" are returned:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;df.select(map_filter(col("ActivityMap"), lambda k,v: array_contains(k, lit("08_S"))))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want returned is &lt;U&gt;only&lt;/U&gt; the key:value elements of the map where the keys are "08_S".&lt;BR /&gt;eg: I want to see&amp;nbsp;{"List(08_S)":[118]} or better yet, {"08_S":118}.&lt;BR /&gt;&lt;BR /&gt;Perhaps&amp;nbsp;the &lt;A href="https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.functions.filter.html" target="_self"&gt;filter&lt;/A&gt;&amp;nbsp;array function could work although I've not yet been successful with it, and clear usage examples are scant.&lt;BR /&gt;&lt;BR /&gt;Any ideas on how to do this?&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;== single cell data map ==&lt;BR /&gt;{"List(08_S, 16_M, 14_O, 06_M, 12_S, 13_S, 05_P, 11_M, 10_P, 23_M, 11_S, 06_S, 01_M, 11_P, 01_S, 06_P, 17_M, 08_M, 09_S, 03_S, 05_S, 03_P, 03_M, 07_M)":[118,47,43,53,22,163,46,22,97,72,43,37,87,42,21,970,509,42,134,115,26,143,20,20]}&lt;/P&gt;</description>
    <pubDate>Wed, 11 Oct 2023 07:14:37 GMT</pubDate>
    <dc:creator>rendorHaevyn</dc:creator>
    <dc:date>2023-10-11T07:14:37Z</dc:date>
    <item>
      <title>Using map_filter / filter to return only map elements for a given predicate</title>
      <link>https://community.databricks.com/t5/get-started-discussions/using-map-filter-filter-to-return-only-map-elements-for-a-given/m-p/48925#M5988</link>
      <description>&lt;P&gt;I want to apply a filter to a map structure (on a column called "ActivityMap") for elements only where a given predicate holds.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Representative data is below.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Applying both "map_filter" and "array_contains" will return rows where a predicate holds, &lt;U&gt;however&lt;/U&gt; the latter returns the entire row cell contents.&lt;BR /&gt;eg: all elements from rows with the key "08_S" are returned:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;df.select(map_filter(col("ActivityMap"), lambda k,v: array_contains(k, lit("08_S"))))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want returned is &lt;U&gt;only&lt;/U&gt; the key:value elements of the map where the keys are "08_S".&lt;BR /&gt;eg: I want to see&amp;nbsp;{"List(08_S)":[118]} or better yet, {"08_S":118}.&lt;BR /&gt;&lt;BR /&gt;Perhaps&amp;nbsp;the &lt;A href="https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.functions.filter.html" target="_self"&gt;filter&lt;/A&gt;&amp;nbsp;array function could work although I've not yet been successful with it, and clear usage examples are scant.&lt;BR /&gt;&lt;BR /&gt;Any ideas on how to do this?&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;== single cell data map ==&lt;BR /&gt;{"List(08_S, 16_M, 14_O, 06_M, 12_S, 13_S, 05_P, 11_M, 10_P, 23_M, 11_S, 06_S, 01_M, 11_P, 01_S, 06_P, 17_M, 08_M, 09_S, 03_S, 05_S, 03_P, 03_M, 07_M)":[118,47,43,53,22,163,46,22,97,72,43,37,87,42,21,970,509,42,134,115,26,143,20,20]}&lt;/P&gt;</description>
      <pubDate>Wed, 11 Oct 2023 07:14:37 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/using-map-filter-filter-to-return-only-map-elements-for-a-given/m-p/48925#M5988</guid>
      <dc:creator>rendorHaevyn</dc:creator>
      <dc:date>2023-10-11T07:14:37Z</dc:date>
    </item>
  </channel>
</rss>

