Load data from a CSV file

These examples illustrate how to load data from a CSV file using the Pandas and GeoPandas libraries.

From latitude and longitude columns


In [1]:
from pandas import read_csv
from geopandas import GeoDataFrame, points_from_xy

remote_file_path = 'http://data.sfgov.org/resource/wg3w-h783.csv'

df = read_csv(remote_file_path)

# Clean rows where the `longitude` column is NULL
df = df[df['longitude'].notna()]

gdf = GeoDataFrame(df, geometry=points_from_xy(df['longitude'], df['latitude']))
gdf.head()


Out[1]:
incident_datetime incident_date incident_time incident_year incident_day_of_week report_datetime row_id incident_id incident_number cad_number ... :@computed_region_qgnn_b9vv :@computed_region_26cr_cadq :@computed_region_ajp5_b2md :@computed_region_nqbw_i6c3 :@computed_region_2dwj_jsy4 :@computed_region_h4ep_8xdi :@computed_region_y6ts_4iup :@computed_region_jg9y_a9du :@computed_region_6pnf_4xz7 geometry
0 2019-05-01T01:00:00.000 2019-05-01T00:00:00.000 01:00 2019 Wednesday 2019-06-12T20:27:00.000 81097515200 810975 190424067 191634131.0 ... 10.0 7.0 35.0 NaN NaN NaN NaN NaN 1.0 POINT (-122.49963 37.76257)
1 2019-06-22T07:45:00.000 2019-06-22T00:00:00.000 07:45 2019 Saturday 2019-06-22T08:05:00.000 81465564020 814655 190450880 191730737.0 ... 1.0 10.0 34.0 1.0 NaN 1.0 NaN NaN 2.0 POINT (-122.40816 37.78054)
2 2019-06-03T16:16:00.000 2019-06-03T00:00:00.000 16:16 2019 Monday 2019-06-03T16:16:00.000 80769875000 807698 190397016 191533509.0 ... 2.0 9.0 1.0 NaN NaN NaN NaN NaN 2.0 POINT (-122.39075 37.72160)
3 2018-11-16T16:34:00.000 2018-11-16T00:00:00.000 16:34 2018 Friday 2018-11-16T16:34:00.000 73857915041 738579 180870806 183202539.0 ... 6.0 3.0 6.0 NaN 18.0 NaN NaN NaN 2.0 POINT (-122.40488 37.79486)
4 2019-05-27T02:25:00.000 2019-05-27T00:00:00.000 02:25 2019 Monday 2019-05-27T02:55:00.000 80509204134 805092 190378555 191470256.0 ... 4.0 6.0 13.0 NaN NaN NaN NaN NaN 1.0 POINT (-122.43056 37.79772)

5 rows × 37 columns


In [2]:
from cartoframes.viz import Layer

Layer(gdf)


Out[2]:
:
StackTrace
    ">

    From a WKT column

    
    
    In [3]:
    from cartoframes.utils import decode_geometry
    
    remote_file_path='http://libs.cartocdn.com/cartoframes/files/starbucks_brooklyn_geocoded.csv'
    
    df = read_csv(remote_file_path)
    
    gdf = GeoDataFrame(df, geometry=decode_geometry(df['the_geom']))
    gdf.head()
    
    
    
    
    Out[3]:
    the_geom cartodb_id field_1 name address revenue id_store geometry
    0 0101000020E61000005EA27A6B607D52C01956F146E655... 1 0 Franklin Ave & Eastern Pkwy 341 Eastern Pkwy,Brooklyn, NY 11238 1321040.772 A POINT (-73.95901 40.67109)
    1 0101000020E6100000B610E4A0847D52C0B532E197FA49... 2 1 607 Brighton Beach Ave 607 Brighton Beach Avenue,Brooklyn, NY 11235 1268080.418 B POINT (-73.96122 40.57796)
    2 0101000020E6100000E5B8533A587F52C05726FC523F4F... 3 2 65th St & 18th Ave 6423 18th Avenue,Brooklyn, NY 11204 1248133.699 C POINT (-73.98976 40.61912)
    3 0101000020E61000008BA6B393C18152C08D62B9A5D550... 4 3 Bay Ridge Pkwy & 3rd Ave 7419 3rd Avenue,Brooklyn, NY 11209 1185702.676 D POINT (-74.02744 40.63152)
    4 0101000020E6100000CEFC6A0E108052C080D4264EEE4B... 5 4 Caesar's Bay Shopping Center 8973 Bay Parkway,Brooklyn, NY 11214 1148427.411 E POINT (-74.00098 40.59321)
    
    
    In [4]:
    Layer(gdf)
    
    
    
    
    Out[4]:
    :
    StackTrace
      ">