Home > database >  How do I combine repeating columns, appending the values from merged columns
How do I combine repeating columns, appending the values from merged columns

Time:11-17

I have an output of a dataframe below, into a dictionary.

{0: ['RevitCategory', 'Door'],
 1: ['DesignModelID', 'ModelA_Rev1'],
 2: ['DesignObjectID', 'ModelA_Rev1_Object1'],
 3: ['TypeName', 'ARC_DOR_INTERNAL'],
 4: ['Function', 'Internal'],
 5: ['Uniclass2015_Ss', 'Ss_25_30_20_25 : Doorset systems'],
 6: ['IfcExportAs', 'IfcDoorType.DOOR'],
 7: ['NRM1Classification', '2.8.1.1 Internal doors'],
 8: ['RevitCategory', 'Wall'],
 9: ['DesignModelID', 'ModelA_Rev1'],
 10: ['DesignObjectID', 'ModelA_Rev1_Object2'],
 11: ['TypeName', 'ARC_WALL_PARTITION_TYPE1'],
 12: ['Function', 'Internal'],
 13: ['Uniclass2015_Ss', 'Ss_25_10_30_35 : Gypsum board partition systems'],
 14: ['IfcExportAs', 'IfcWallType.PARTITIONING'],
 15: ['NRM1Classification', '2.7.1.1 Internal walls'],
 16: ['Area', 5],
 17: ['RevitCategory', 'Wall'],
 18: ['DesignModelID', 'ModelA_Rev1'],
 19: ['DesignObjectID', 'ModelA_Rev1_Object3'],
 20: ['TypeName', 'ARC_WALL_PARTITION_TYPE1'],
 21: ['Function', 'Internal'],
 22: ['Uniclass2015_Ss', 'Ss_25_10_30_35 : Gypsum board partition systems'],
 23: ['IfcExportAs', 'IfcWallType.PARTITIONING'],
 24: ['NRM1Classification', '2.7.1.1 Internal walls'],
 25: ['Area', 5],
 26: ['RevitCategory', 'Wall'],
 27: ['DesignModelID', 'ModelA_Rev1'],
 28: ['DesignObjectID', 'ModelA_Rev1_Object4'],
 29: ['TypeName', 'ARC_WALL_PARTITION_TYPE1'],
 30: ['Function', 'Internal'],
 31: ['Uniclass2015_Ss', 'Ss_25_10_30_35 : Gypsum board partition systems'],
 32: ['IfcExportAs', 'IfcWallType.PARTITIONING'],
 33: ['NRM1Classification', '2.7.1.1 Internal walls'],
 34: ['Area', 5]

How would I combine the columns, for example in an output like the one below, stacking the results of the columns as they are merged ?

RevitCategory  | DesignModelID  | DesignObjectID          |  etc. 
-----------------------------------------------------------------               
Door           | ModelA_Rev1    |  ModelA_Rev1_Object1    |
Wall           | ModelA_Rev1    |  ModelA_Rev1_Object1    |
Wall           | ModelA_Rev1    |  ModelA_Rev1_Object1    |
-----------------------------------------------------------------     

      

CodePudding user response:

you could create records and create the dataframe with DataFrame.from_records

obj = {0: ['RevitCategory', 'Door'],
 1: ['DesignModelID', 'ModelA_Rev1'],
 2: ['DesignObjectID', 'ModelA_Rev1_Object1'],
 3: ['TypeName', 'ARC_DOR_INTERNAL'],
 4: ['Function', 'Internal'],
 5: ['Uniclass2015_Ss', 'Ss_25_30_20_25 : Doorset systems'],
 6: ['IfcExportAs', 'IfcDoorType.DOOR'],
 7: ['NRM1Classification', '2.8.1.1 Internal doors'],
 8: ['RevitCategory', 'Wall'],
 9: ['DesignModelID', 'ModelA_Rev1'],
 10: ['DesignObjectID', 'ModelA_Rev1_Object2'],
 11: ['TypeName', 'ARC_WALL_PARTITION_TYPE1'],
 12: ['Function', 'Internal'],
 13: ['Uniclass2015_Ss', 'Ss_25_10_30_35 : Gypsum board partition systems'],
 14: ['IfcExportAs', 'IfcWallType.PARTITIONING'],
 15: ['NRM1Classification', '2.7.1.1 Internal walls'],
 16: ['Area', 5],
 17: ['RevitCategory', 'Wall'],
 18: ['DesignModelID', 'ModelA_Rev1'],
 19: ['DesignObjectID', 'ModelA_Rev1_Object3'],
 20: ['TypeName', 'ARC_WALL_PARTITION_TYPE1'],
 21: ['Function', 'Internal'],
 22: ['Uniclass2015_Ss', 'Ss_25_10_30_35 : Gypsum board partition systems'],
 23: ['IfcExportAs', 'IfcWallType.PARTITIONING'],
 24: ['NRM1Classification', '2.7.1.1 Internal walls'],
 25: ['Area', 5],
 26: ['RevitCategory', 'Wall'],
 27: ['DesignModelID', 'ModelA_Rev1'],
 28: ['DesignObjectID', 'ModelA_Rev1_Object4'],
 29: ['TypeName', 'ARC_WALL_PARTITION_TYPE1'],
 30: ['Function', 'Internal'],
 31: ['Uniclass2015_Ss', 'Ss_25_10_30_35 : Gypsum board partition systems'],
 32: ['IfcExportAs', 'IfcWallType.PARTITIONING'],
 33: ['NRM1Classification', '2.7.1.1 Internal walls'],
 34: ['Area', 5]}

# create records
records = []
c = None
for k, v in obj.values():
    # check if we have a new record to add
    if k == "RevitCategory":
        # we do not want to add the at the first occurence of RevitCategory
        if c is not None:
            # append current element as record
            records.append(c)
        # reset current record
        c = {}
    # set the value for the key
    c[k] = v

# create dataframe
df = pd.DataFrame.from_records(records)

CodePudding user response:

Unless I misunderstood your problem and depending on if 'RevitCategory' marks the begining of the new row, it could work like this. I don't know if there is a solution more idiomatic to pandas.

df = pd.DataFrame()
j = 0
for key in dict:
    if dict[key][0] == 'RevitCategory':
        row = {dict[key][0]: dict[key][1]}
        i = 1
        while key   i in dict and dict[key   i][0] != 'RevitCategory':            
            row[dict[key   i][0]] = dict[key   i][1]
            i  = 1 
        if not df.empty: 
            df = pd.concat([df, pd.DataFrame(row, index=[j])])
        else:
            df = pd.DataFrame(row, index=[0])   
        j  = 1
  • Related