2

I am using geopandas to read the shapefile of municipalities of Switzerland, i.e municipalities.shp. For 170 I have the info of the population, i.e. population.csv The files from can be found in this repo here.

Is it possible to merge the information trough the so called BFS number.

import pandas as pd
import geopandas

mun = geopandas.read_file('municipalities.shp')
pop = pd.read_csv('population.csv')
## merge data
mergedData = pd.merge(mun,pop,left_on='BFS_NUMMER',right_on='BFS')

Now for each of the 170 municipalities I have the geographic information and the info of the population.

I would like to know if I can use pysal to check if the population of these 170 municipalities is spatially autocorrelated.

emax
  • 6,965
  • 19
  • 74
  • 141

1 Answers1

2

Yes, you can. First of all you need to be sure that you are passing geodataframe, your code is returning pandas dataframe:

import pandas as pd
import geopandas as gpd

mun = gpd.read_file('municipalities.shp')
pop = pd.read_csv('population.csv')
# merge data
mergedData = mun.merge(pop,left_on='BFS_NUMMER',right_on='BFS')

Then you can work with pysal's tools. I will be using libpysal and esda packages following new structure of pysal.

import libpysal
import esda

weights = libpysal.weights.Queen.from_dataframe(mergedData)  # generate spatial weights (Queen in this case)
spatial_auto = esda.Moran(mun[['population']], weights)  # calculate Moran's I

First you have to generate spatial weights matrix. If you want to use different than Queen, just follow https://libpysal.readthedocs.io/en/latest/api.html. Then you generate Moran's I index of spatial autocorrelation (global). It generates all the attributes you might need (https://esda.readthedocs.io/en/latest/generated/esda.Moran.html#esda.Moran). Similar syntax would be for Gamma, Geary's C or Getis Ord autocorrelation indices.

Documentation for esda is really good, showing examples in jupyter notebooks, I recommend to check that for other info (like local autocorrelation or plotting) - https://esda.readthedocs.io/en/latest/.

martinfleis
  • 7,124
  • 2
  • 22
  • 30
  • 2
    Is there a way to calculated spatial correlation between points? It seems this method requires polygons – guyts Jul 26 '19 at 14:14
  • 2
    There is, you just need to define spatial weights matrix in some way. You can certainly use `libpysal.weights.DistanceBand`, `libpysal.weights.Kernel` or `libpysal.weights.KNN` to do that. Example above is using Queen, which is contiguity-based method depending on polygons. Those listed are distance based, so they work on points as well. – martinfleis Jul 26 '19 at 19:54