How to convert KML file to Pandas Dataframe and CSV

Hi all,
I was provided with a KML file and I needed to turn it into a dataframe for further data manipulation with pandas, and then have it on map.
I’m sharing with you the code, in hope it can be useful to someone.

# -----------------------------------------------
#                  LIBRARIES
# -----------------------------------------------

import geopandas as gpd 
import fiona 
import numpy as np
import pandas as pd 

# ------------------------------
# ------------------------------


# READ KML file to a geopandas dataframe 

geo_df = gpd.read_file('your_kml_file.kml',driver='KML')

# Create Pandas Dataframe from GeoPandas 

df= pd.DataFrame(geo_df)

# Extract latitude and longitude from the KML geometry column
df['lat'] = df.geometry.apply(lambda p: p.y)
df['lon'] = df.geometry.apply(lambda p: p.x)

# You now have a pandas dataframe with a lat and lon column that you can use to plot a mapbox map and/or add more data to it. 

# Made with 🤍 by Jorge Gomes MAR 21 2022 

How can you improve this?

  • Make a loop to detect all KML files in a folder and turn them into an unique pandas dataframe
  • Save to .csv using pandas .to_csv function

Hope this can be of use to someone!


No, can’t be CSV. That is a type of non-relational data. You should at least consider storage formats like JSON and XML. And the best place to store data is in the database. Try GeoDatabase, MongoDB or something.

Hi @stu, while I appreciate your comment I don’t see any reason why I shouldn’t use a csv file to store lat and lon data, when that is just part of the data I need in my dataset. Not all of us have the knowledge to use “GeoDatabase, MongoDB or something” and the objective here is to simplify and provide a working solution, not to complicate things even more.

Aha, I see. You are not here to seek advice. I would give a stable and reliable result. The latitude and longitude data is just a description of some points in the KML file. BTW, the geopandas library also has GeoSeries.y, GeoSeries.x, and GeoDataFrame.to_file APIs. And the common usage is gdf.to_file('dataframe.shp') or gdf.to_file('dataframe.geojson', driver='GeoJSON') etc.

Also, I suggest you change the title to How to extract coordinates of points from a KML file and save into CSV.

Hi @jgomes_eu ,
Thank you for sharing your example with us. I appreciate that you took the time to share with the community the knowledge that you gained on KML. That only makes the community a better place where people can learn Dash with greater ease.

I hope to see more of theses :slight_smile:

1 Like

Hi @jgomes_eu,

Thanks for sharing this solution to working with KML files. This is by far the easiest solution I’ve seen. Thanks for your time!


Hi Jorge,
I am getting an Error Message in Pycharm:

Traceback (most recent call last):
for line:[‘KML’] = ‘rw’
AttributeError: ‘NoneType’ object has no attribute ‘drvsupport’

Is there a requirement for specific versions of geopandas or fiona?

Hi @piet,

In order to avoid the error message, you must change the line:[‘KML’] = ‘rw’
to:[‘LIBKML’] = ‘rw’
and later:
geo_df = gpd.read_file(kmlFile, driver='LIBKML')

I think in @igomes_eu’s code, you should add the next line:
import geopandas as gpd



Thanks! Very helpful.