Apologies for the long subject, but I hope I have captured the intention of this question
This question is a continuation of Python pandas to ensure each row based on column value has a set of data present, if not add row
I have a CSV file with the following values:
resource_id,resource_type,tag_key,tag_value
vol-00441b671ca48ba41,volume,Environment,Development
vol-00441b671ca48ba41,volume,Name,Database Files
vol-00441b671ca48ba41,volume,Project,Application Development
vol-00441b671ca48ba41,volume,Purpose,Web Server
vol-00441b671ca48ba41,volume,Management,Internal web team
i-1234567890abcdef0,instance,Environment,Production
i-1234567890abcdef0,instance,Owner,Fast Company
i-1234567890abcdef0,instance,Random,Do not remove
I wish to append additional tag_key values for each resource_id, they are:
taglist = ['Application',
           'Client',
           'Environment',
           'Name',
           'Owner',
           'Project',
           'Purpose']
I have achieved 90% of what I am after, and taglist becomes ['Application' 'Client' 'Environment' 'Management' 'Name' 'Owner'
 'Project' 'Purpose' 'Random']
However, this was applied to each resource_id 
Some tag_key are present only for some resource_id
import pandas as pd
import numpy as np
file_name = "z.csv"
file_name_output = "x.csv"
column_header = ['resource_id', 'resource_type', 'tag_key', 'tag_value']
df = pd.read_csv(file_name, names=column_header)
taglist = ['Application',
           'Client',
           'Environment',
           'Name',
           'Owner',
           'Project',
           'Purpose']
taglist_present = df['tag_key'].unique()
taglist = np.unique(np.concatenate((taglist,taglist_present),0))
print (taglist)
df_out = df.set_index(['resource_id','tag_key'])\
           .reindex(pd.MultiIndex.from_product([df['resource_id'].unique(), taglist],
                                              names=['resource_id','tag_key']))
df_new = df_out.assign(resource_type = df_out.groupby('resource_id')['resource_type']\
                                              .ffill().bfill()).reset_index()
df_new = df_new.reindex(columns=column_header)
print(df_new)
df_new.to_csv(file_name_output, index=False)
Expected results:
              resource_id resource_type      tag_key                tag_value
0   vol-00441b671ca48ba41        volume  Application                      NaN
1   vol-00441b671ca48ba41        volume       Client                      NaN
2   vol-00441b671ca48ba41        volume  Environment              Development
3   vol-00441b671ca48ba41        volume   Management        Internal web team
4   vol-00441b671ca48ba41        volume         Name           Database Files
5   vol-00441b671ca48ba41        volume        Owner                      NaN
6   vol-00441b671ca48ba41        volume      Project  Application Development
7   vol-00441b671ca48ba41        volume      Purpose               Web Server
9     i-1234567890abcdef0      instance  Application                      NaN
10    i-1234567890abcdef0      instance       Client                      NaN
11    i-1234567890abcdef0      instance  Environment               Production
13    i-1234567890abcdef0      instance         Name                      NaN
14    i-1234567890abcdef0      instance        Owner             Fast Company
15    i-1234567890abcdef0      instance      Project                      NaN
16    i-1234567890abcdef0      instance      Purpose                      NaN
17    i-1234567890abcdef0      instance       Random            Do not remove
Actual results:
              resource_id resource_type      tag_key                tag_value
0   vol-00441b671ca48ba41        volume  Application                      NaN
1   vol-00441b671ca48ba41        volume       Client                      NaN
2   vol-00441b671ca48ba41        volume  Environment              Development
3   vol-00441b671ca48ba41        volume   Management        Internal web team
4   vol-00441b671ca48ba41        volume         Name           Database Files
5   vol-00441b671ca48ba41        volume        Owner                      NaN
6   vol-00441b671ca48ba41        volume      Project  Application Development
7   vol-00441b671ca48ba41        volume      Purpose               Web Server
8   vol-00441b671ca48ba41        volume       Random                      NaN
9     i-1234567890abcdef0      instance  Application                      NaN
10    i-1234567890abcdef0      instance       Client                      NaN
11    i-1234567890abcdef0      instance  Environment               Production
12    i-1234567890abcdef0      instance   Management                      NaN
13    i-1234567890abcdef0      instance         Name                      NaN
14    i-1234567890abcdef0      instance        Owner             Fast Company
15    i-1234567890abcdef0      instance      Project                      NaN
16    i-1234567890abcdef0      instance      Purpose                      NaN
17    i-1234567890abcdef0      instance       Random            Do not remove
 
     
    