Friday, September 25, 2020

Mapping dictionary keys/values to pandas dataframe

 This is a senario where we have a dataframe representing states and their geo-political zones. The geo-political zone column is however abbreviated as seen below, so we want to have a new column that will hold the full meaning of the corresponding abbreviation.


The abbreviated letters are defined in dictionary, which will be used to map the new full meaning column.
look_up_dict = {'NC': 'North Central', 'NE': 'North East', 'NW': 'North West', 'SE': 'South East', 'SW': 'South West', 'SS': 'South South'}

The lookup dictionary could come from a different dataframe, that is to say the dataframe can be converted to dictionary for this lookup purpose.

A solution here was to use the map() function to create a new column by mapping the dictionar keys to the values on geopolitical zone column 'GeoPZ'

df['Geo Political Zones'] = df['GeoPZ'].map(look_up_dict)

How about if we just want to replace the existing 'GeoPZ' column without creating a new one? There are couple of ways to get this done by using replace() or update() methods or simply by overriding the existing column.

df['GeoPZ'] = df['GeoPZ'].map(look_up_dict)

df['GeoPZ'] = df['GeoPZ'].replace(look_up_dict)

To use update() method, your dictionary keys must be numeric indices. Use .keys() to check your dictionary keys.
df.GeoPZ.update(pd.Series(look_up_dict))


Lambda function

You can use a Lamda function to perform operations on the fly while creating a new column. Lets say, we want a new column that will hold character count/lenght of each state's name. Then a Lamda function will come in handy as seen below;-

df['State_LCount'] = df['State'].map(lambda x: len(str(x)))

Thank you for following!

Tuesday, September 22, 2020

ColorBrewer for map designers

This article is about choosing a friendly colors for your next map design.

Random color ramp

Greens color ramp

Most modern mapping software will give you great color ideas for your map. However, the problem with the software suggested color is that there are only a handful of those colors and in some cases you will like to manually setup your colors since the default color ranges may not fit into your design.



If you find yourself in this situation and you are struggling to combine colors in a color friendly manner, then you are in the right place.

Here I will talk about a tool that help solve this problem and at the end you should be comfortable selecting proper colors for your map.

To tool is called ColorBrewer. It is a web based tool that provides "Color Advice for Maps".


The tool provides different properties to select based on your map. For example number of data class for your map, nature of the data (Sequential, Diverging and Qualitative). And many other properties as seen on the website.

Some other tools to help you combine friendly colors are:-











3) Flat UI Color Picker





Study the tools and choose the perfect friendly color for your next map.

Happy mapping!

Sunday, September 20, 2020

Recreating NCDC Covid-19 GIS Maps

Ever since the first case of Coronavirus disease (COVID-19) was announced in Nigeria on 27th of February 2020, the Nigeria Centre for Disease Control (NCDC) has in its effort to control the pandemic produced maps to document and visualize the status of the disease.

Some of these maps are part of the online 'COVID-19 Cases Tracking Dashboard' while other are included in pdf reports on 'update of COVID-19 outbreak' in Nigeria as seen below.


When you download and open one of the daily reporst, you will see the map below...



The second map is on the 'COVID-19 Cases Tracking Dashboard' web page.


Thursday, September 17, 2020

PyQGIS - Get attribute column names and dataType

 Ocationally, I did like to check the attribute fields/columns of a layer I am working on. Sometimes to get their names, types or just to get the count of how many fields/columns are on that layer.

You could access some of these information when you open the layer's property window, under the information tab as seen below.


Thursday, September 10, 2020

Pandas Dataframe to Python Dictionary

Assuming we have this table of "Confirmed COVID-19 Cases in Nigeria" as seen below and we want to convert either the whole table or some of its columns to a python dictionary. If you don't know already, a python dictionary is a data structure that consists of a collection of key-value pairs.



The column are as follow:-

  1. Column A: States Affected
  2. Column B: No. of Cases (Lab Confirmed)
  3. Column C: No. of Cases (on admission)
  4. Column D: No. Discharged
  5. Column E: No. of Deaths