<?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 Convert JSON to GeoDataFrame and append both geometry and filename in Developers Questions</title>
    <link>https://community.esri.com/t5/developers-questions/convert-json-to-geodataframe-and-append-both/m-p/1140210#M6159</link>
    <description>&lt;P&gt;I am currently running an experiment on automating a task by merging information from various files types and doing a number of operation to achieve this.&lt;/P&gt;&lt;P&gt;Currently, I am reading a a list of GeoJSONs and trying to build a GeoDataFrame that contains both the geometry and the filename.&lt;/P&gt;&lt;P&gt;Example of file name structure in folder is: event1.json&lt;/P&gt;&lt;P&gt;The desired geodataframe should look like this:&lt;/P&gt;&lt;PRE&gt;    index     geometry                      event_name
    0         LINESTRING1(coordinates...)   event1
    1         LINESTRING2(coordinates...)   event2
    2         LINESTRING3(coordinates...)   event3&lt;/PRE&gt;&lt;P&gt;I am able to read the JSONs and create the geometries, but I am not able to assign the matching filename.&lt;/P&gt;&lt;PRE&gt;     import os, geopandas as gpd

     event_tracks = gpd.GeoDataFrame(columns=['geometry', 'eventname'])

     directory = os.getcwd()

     
     for dir_root, dir_dir, js_file  in os.walk(directory):
       for f in js_file:
         if f.endswith(".json"):
         fname = os.path.basename(f)
         fname, fext = fname.split('.')
         print (fname)
         print ('Reading Track Path JSON ' + f)
         with open(f, 'r') as path_f:
           event_tracks = gpd.read_file(path_f).append(event_tracks)&lt;/PRE&gt;&lt;P&gt;The code above works for geometries and generates a geodataframe with valid geometries. However, I cannot assign the event name into the column. So far, I've&lt;A href="https://www.olansith.com/" target="_self"&gt;&amp;nbsp;&lt;/A&gt;tried to:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Create list of names and append list elements&lt;/LI&gt;&lt;LI&gt;Reindex and concatenate two different dataframes&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;However, it either returns a NaN-filled column or an error such as: TypeError: cannot concatenate object of type '&amp;lt;class 'list'&amp;gt;'; only Series and DataFrame objs are valid.&lt;/P&gt;&lt;P&gt;Is there any workaround to do this?&lt;/P&gt;</description>
    <pubDate>Thu, 17 Feb 2022 12:00:01 GMT</pubDate>
    <dc:creator>TamnaSidhart</dc:creator>
    <dc:date>2022-02-17T12:00:01Z</dc:date>
    <item>
      <title>Convert JSON to GeoDataFrame and append both geometry and filename</title>
      <link>https://community.esri.com/t5/developers-questions/convert-json-to-geodataframe-and-append-both/m-p/1140210#M6159</link>
      <description>&lt;P&gt;I am currently running an experiment on automating a task by merging information from various files types and doing a number of operation to achieve this.&lt;/P&gt;&lt;P&gt;Currently, I am reading a a list of GeoJSONs and trying to build a GeoDataFrame that contains both the geometry and the filename.&lt;/P&gt;&lt;P&gt;Example of file name structure in folder is: event1.json&lt;/P&gt;&lt;P&gt;The desired geodataframe should look like this:&lt;/P&gt;&lt;PRE&gt;    index     geometry                      event_name
    0         LINESTRING1(coordinates...)   event1
    1         LINESTRING2(coordinates...)   event2
    2         LINESTRING3(coordinates...)   event3&lt;/PRE&gt;&lt;P&gt;I am able to read the JSONs and create the geometries, but I am not able to assign the matching filename.&lt;/P&gt;&lt;PRE&gt;     import os, geopandas as gpd

     event_tracks = gpd.GeoDataFrame(columns=['geometry', 'eventname'])

     directory = os.getcwd()

     
     for dir_root, dir_dir, js_file  in os.walk(directory):
       for f in js_file:
         if f.endswith(".json"):
         fname = os.path.basename(f)
         fname, fext = fname.split('.')
         print (fname)
         print ('Reading Track Path JSON ' + f)
         with open(f, 'r') as path_f:
           event_tracks = gpd.read_file(path_f).append(event_tracks)&lt;/PRE&gt;&lt;P&gt;The code above works for geometries and generates a geodataframe with valid geometries. However, I cannot assign the event name into the column. So far, I've&lt;A href="https://www.olansith.com/" target="_self"&gt;&amp;nbsp;&lt;/A&gt;tried to:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Create list of names and append list elements&lt;/LI&gt;&lt;LI&gt;Reindex and concatenate two different dataframes&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;However, it either returns a NaN-filled column or an error such as: TypeError: cannot concatenate object of type '&amp;lt;class 'list'&amp;gt;'; only Series and DataFrame objs are valid.&lt;/P&gt;&lt;P&gt;Is there any workaround to do this?&lt;/P&gt;</description>
      <pubDate>Thu, 17 Feb 2022 12:00:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/developers-questions/convert-json-to-geodataframe-and-append-both/m-p/1140210#M6159</guid>
      <dc:creator>TamnaSidhart</dc:creator>
      <dc:date>2022-02-17T12:00:01Z</dc:date>
    </item>
  </channel>
</rss>

