How to Create One Category from 2 Datasets in Python
<p>I have two data sets that both have data in  it like follows:</p>

<p>Data set 1:</p>

<pre><code>Country Year   Cause  Gender    Deaths
2090    2011    A000    1       70340
2090    2010    A001    2       53449
2090    2009    A002    1       1731
2090    2008    A003    2       1270
2090    2007    A004    1       148
2310    2011    A000    2       172
2310    2010    A001    1       24
2310    2009    A002    2       20
2310    2008    A003    1       27
2660    2013    A004    2       21
2660    2012    A005    1       88
2660    2011    A006    2       82
</code></pre>

<p>Data set 2:</p>

<pre><code>Country Year   Cause  Gender    Deaths
2090    1999    B529    1       557
2090    1995    A001    2       234
2090    1996    B535    1       29
2090    1997    A002    2       33
2090    1998    B546    1       3224
2090    1999    B556    2       850
2310    1995    B555    1       319
2310    1996    A003    2       143
2310    1997    B563    1       251
2310    1998    B573    2       117
2660    1997    B561    1       244
2660    1998    A002    2       115
2660    1999    A001    1       10
2660    2000    B569    2       2
</code></pre>

<p>I need to create categories on the Cause column codes which are for causes of death. But I need to make this category from using these combined causes from both data sets separately e.g.</p>

<p>Road Traffic Accidents Category: From Data set 1: A001, A003</p>

<p>Road Traffic Accidents Category: From Data set 2: B569, B555</p>

<p>and the causes from both of these must be included in the Road Traffic Accidents Category.</p>

<p>They must be included in each category for each data set (not combined) like: Road Traffic Accidents: A001, A003, B569, B555</p>

<p>This is because say for example A001. In Data set 1 A001 is for Car Accidents, but in Data set 2 A001 means Heart Attack and I don't want Heart Attack in the Road Traffic Accidents category. But when the category is made from both data sets (i.e. Road Traffic Accidents: A001, A003, B569, B555) then A001 from both data sets is included in the Road Traffic Accidents category. </p>

<p>The purpose of this question is to see how different categories differ over the years in terms of deaths - I am not allowed to combine both data sets manually not on Python. I am also not allowed to use any of the common packages such as Pandas, Numpy, etc.</p>

<p>Thank you for help in advanced </p>

<p>So my understanding of your problem is (correct me if I'm wrong), you have two datasets which both have a "Cause" column/variable. But the encoding of this "Cause" column in two datasets are different. </p>

<p>In Dataset1, perhaps the encoding says:</p>

<pre><code>Road Traffic Accidents Category: A001, A003
Heart Attack Category: C001, C002  #made up encoding
</code></pre>

<p>In Dataset2, perhaps the encoding says:</p>

<pre><code>Road Traffic Accidents Category:  B569, B555
Heart Attack Category: A001
Hurricane Cause of Death Category: E941 # made up encoding
</code></pre>

<p>What you want is to create a consistent category to cause encoding mapping which works for two datasets. </p>

<p>I personally think python dictionary is the right data structure for this task. I assume you can load the category-cause mapping for both datasets. </p>

<pre><code>data1_cat_cause = {'Road Traffic Accidents': ['A001', 'A003'],
                   'Heart Attack': ['C001', 'C002']}
data2_cat_cause = {'Road Traffic Accidents': ['B569', 'B555'],
                   'Heart Attack': ['A001'], 
                   'Hurricane Cause of Death': ['E941']}

category_combined = set(data1_cat_cause.keys()) | set(data2_cat_cause.keys())
cat_cause_combined = {}
for category in category_combined:
    cat_cause_combined[category] = {'Dataset1':data1_cat_cause.get(category),'Dataset2':data2_cat_cause.get(category)}
</code></pre>

<p>This would yield following information stored in the "cat_cause_combined" variable:</p>

<pre><code>                          Dataset1 encoding       Dataset2 encoding
Road Traffic Accidents  :  ['A001', 'A003']       ['B569', 'B555']
Heart Attack            :  ['C001', 'C002']       ['A001']
Hurricane Cause of Death:   None                  ['E941']
</code></pre>

<p>I hope I understand your problem correctly and I hope this solution solves your problem. </p>

python dataset category jupyter-notebook