Sunday, November 12, 2017

Pandas Merge two Data frames based on common column values

Sometime back I had this "Combining multiple tabular data files together" situation at hand and I wrote about it then.

Now, there is another problem I have to solve.... It is similar to the above but not exactly the same. I will explain it below:-

The Problem

I have tow Data frames (tabular data) in which on is a subset of the other. That is to say second data frame (df2) is a subset of first data frame (df1).

Now, I need to merge them together based on a common column in the two data frames (df1 and df2) and also keep track of what row was in the the main data frame and not in the subset data frame. This is more like saying:
- Remove rows from two Data frames that have uncommon column value
- To find rows in one data frame but not in another
- Find rows which don't exist in another data frame by multiple columns

Any how you look at it, above is the general idea!


The Solution

I solved this by using the pandas merge() method with the following flags/parameters: "how='left'" and "indicator=True".



This created a Data frame with an extra column name: _merge that contains two keywords 'both' (row contain in both data frames - df1 and df2) and 'left_only' (row contain in only the left data frames - df2).



Example:-

df_all = df1.merge(df2.drop_duplicates(), on=['col1','col2'], how='left', indicator=True)
      


That is it!

Wednesday, November 1, 2017

QGIS - Label Multiple Polygons with one attribute text


QGIS Label group of polygons with single text fro the attribute field


Suppose you have a vector layer that contains polygons with the same attribute values as seen below.




You don't want to name each of the polygons because the values are the same, instead you just want one name for the whole group of polygon as seen below...



To achieve this, create a new attribute field in the to control either the label string is turned on/show or off/hide the labels by assigning the value of 1 or 0.

1 is for: On/Show Label
0 is for: Off/Hide Label


Now, after creating the attribute field, take note of the field name and go to the layers label property tab. Define and enable the field label as usual and under: Rendering >> Data Define >> Show Label, select the label control field you created above and save your changes.



All the polygons with the value of "0" will have there labels turned off/hidden only those with value of "1" will be on/showing.


Note: There are many ways to do this, the above is perfect if you want to have control over your label within QGIS map canvas.


That is it.
Thank you for reading.