'Line2D' object has no property 'column'

Viewed 7800

I would to like create a simple choropleth map of the world with respect to pisa score of different countries. Hence my DataFrame looks like:

merged = merged[['geometry','Avg_score']]
merged.head()

My DataFrame

To plot choropleth map:

fig, ax = plt.subplots(1,figsize=(15, 10))


ax.axis('off')
ax.set_title('Education Ranking', fontdict={'fontsize': '35', 'fontweight' : '3'})
merged.plot(column='Avg_score', cmap='Blues_r', scheme='User_Defined',
                    classification_kwds=dict(bins=[325,416,463,499,591]),
                     edgecolor = 'black', linewidth=0.7, ax=ax,legend= True)
leg = ax.get_legend()
leg.set_bbox_to_anchor((1, 0.7, 0.2, 0.2))
 plt.show()

However, I get the following error: 'Line2D' object has no property 'column'. A similar post can be found here: 'Line2D' object has no property 'kind' . In anycase this problem is not fully addressed here! Any help would be highly appreciated as I am stuck here far too long.

2 Answers

I was running into this exact same issue.

Short Answer

Try converting the merged dataframe into a geodataframe

from geopandas import GeoDataFrame

merged = GeoDataFrame(merged)

Long Answer

Since the error said there was a property that my merged object didn't have, I tried checking the type of the merged object. I found that after I merged a dataframe with a geodataframe, the resultant object was a dataframe (not a geodataframe).

Try checking to see the type of your merged data object by using

type(merged)

If it is a dataframe object and not a geodataframe object, geopandas won't know how to make your map. This dataframe can be easily converted to a geodataframe then plotted by using the

First, make sure to import GeoDataFrame to convert dataframes into geodataframes,

from geopandas import GeoDataFrame

Then convert the dataframe to a geodataframe,

merged = GeoDataFrame(merged)

The issue was resolved, when instead of using

merged = df.merge(map_df,how = 'left', on = 'Country')

to arrive at the merged DataFrame, I used

merged = map_df.set_index('COUNTRY').join(df.set_index('Country')).

Still not sure why this works over the other. However, did not face any issues after this change!

Related