Creating a new column based on the values from another column in Pandas

#Creating-a-new-column-based-on-the-values-from-another-column-in-Pandas

Let's import the pandas libary.

I am importing a table of contents so it is easy to understand how the new column is created.

In this case I am importing the list of monarchies published on wikipedia:

https://en.wikipedia.org/wiki/List_of_monarchies

Loading output library...

I delete all the columns that I dont need for this example and I add the 'Realm / Kingdom' column as an index of the table to make it clearer.

Loading output library...

Now it is time to create the new column based on the values from another column.

In this case, I want to create a new column called 'monarchy type' based on the values from the column 'Type'.

So, for example, if monarchy type is 'Constitutional' the new column will have a value 1, 2 if it is 'mixed' and so on.

First I want to list all unique values for the 'Type' column.

Loading output library...

I specify the value for each type of monarchy.

Loading output library...

Done!!!

I create a new column based on the values from another column.

Here is an extended example where I apply this code - https://kyso.io/helenadomo/monarchy