<jupyter_start><jupyter_text>Convert a pandas DataFrame to GeoJSON (Stations) In this Python snippet we use stations (geographic) from the Swiss public transportation and convert the data to a GeoJSON (http://geojson.org/) file.- Donwload the _'bfkoordgeo.csv'_ file, https://opentransportdata.swiss/en/dataset/bhlist/resource/b92a372f-7843-4ddd-b1c6-c9c6397e1097- Convert the file from ISO-8859-1 to UTF-8```iconv -f ISO-8859-1 -t UTF-8 bfkoordgeo.csv > out.csv```> __Note:__ The data is from the _Open Data Platform Swiss Public Transport_, https://opentransportdata.swiss/en/ Required libraries- pandas, http://pandas.pydata.org/- geojson, https://pypi.python.org/pypi/geojson/  Load the dataFirst let's load the data with pandas. The data frame contains the stations from the public transportations from Switzerland and some from adjoining countries. We have the columns:- StationID- Longitude- Latitude- Height- RemarkLongitude and Latitude should be _WGS 84_ coordinates.<jupyter_code>import pandas as pd

df = pd.read_csv('data/bfkoordgeo_utf8.csv')
df.head()<jupyter_output><empty_output><jupyter_text>Now we do some data cleaning and remove all rows where _Longitude_ and _Latitude_ are _'null'_.<jupyter_code>df = df[df['Longitude'].notnull()]
df = df[df['Latitude'].notnull()]

# will display all rows that have null values
#df[df.isnull().any(axis=1)]<jupyter_output><empty_output><jupyter_text>Convert pandas data frame to GeoJSONNext we convert the panda data frame to geosjon objects (_FeatureCollection/Feature/Point_).<jupyter_code>import geojson as geojson

values = zip(df['Longitude'], df['Latitude'], df['Remark'])
points = [geojson.Feature(geometry=geojson.Point((v[0], v[1])), properties={'name': v[2]}) for v in values]

geo_collection = geojson.FeatureCollection(points)

print(points[0])<jupyter_output>{"geometry": {"coordinates": [7.549783, 47.216111], "type": "Point"}, "properties": {"name": "<NAME>"}, "type": "Feature"}<jupyter_text>Save the GeoJSON (FeatureCollection)  to a fileFinally we dump the GeoJSON objects to a file.<jupyter_code>dump = geojson.dumps(geo_collection, sort_keys=True)

'''
with open('stations.geojson', 'w') as file:
    file.write(dump)
'''<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12791741472172352, 'fraction_numerical': 0.025134649910233394, 'mean_word_length': 6.237012987012987, 'pattern_counts': {'":': 6, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '377702', 'license': 'MIT', 'n_tokens_mistral': 779, 'n_tokens_neox': 725, 'n_words': 208, 'path': 'geojson/geojson_stations.ipynb', 'repo_name': 'rueedlinger/python-snippets', 'size': '2236'}