Reputation: 1439
I am playing with the following shapefile that can be found here on Github. I'm not going to include the output but here is the code that reads it in as a geopandas df and plots the shapefile.
states = geopandas.read_file('usa-states-census-2014.shp')
states.head()
states.plot()
I also have the following df, which is the USArrests
dataset which can be found here on Kaggle.
What I am trying to do is merge the two datasets so I can plot the shapefile with a colormap of the features in USArrests
. I ran the following code to merge the datasets.
df = pd.read_csv("USArrests.csv")
df.rename(columns = {'Unnamed: 0':'NAME'}, inplace = True)
merged = pd.merge(df, states, on=['NAME']).drop_duplicates(subset=['NAME'])
The merge worked, but now when I run states.plot()
I get a line plot, and I'm assuming this is because the df is no longer a GeoDataFrame
.
My quesiton is, how can I take the data from USArrests
and use it to plot a colormap over the shapefile? Thanks!
Note, I am using the following guide to help me but it doesn't cover my specific task
Upvotes: 1
Views: 3959
Reputation: 7804
Use GeoDataFrame.merge
method. That way it remains GeoDataFrame. What happened in your case is that merged
is pandas.DataFrame only.
merged = states.merge(df, on='NAME').drop_duplicates(subset=['NAME'])
I am guessing the correct form a bit as you did not show the structure of any of your data frames. See the documentation for more https://geopandas.readthedocs.io/en/latest/docs/user_guide/mergingdata.html?highlight=merge#attribute-joins
Upvotes: 4