I have an empty dataframe with 30 col, I am parsing each file and extracting the data extracting the metadata into a dictionary, the keys of dictionary match the col headers of dataframe, the number of keys in the dictionary depends on whats available in the file, how to insert a row into the dataframe based on values in dictionary?
Data in File:
Col1 Col2 Col3
PD . DD: PERMANENT DATUM
LMF . RT: LOG MEASURED FROM
DAPD.FT 98: FEET ABOVE PERMANENT DATUM
DMF . RT: DRILLING MEASURED FROM
EKB .FT 100: KELLY BUSHING
EGL .FT -500: GROUND LEVEL
DATE. 08/12/95: RUN DATE
RUN . 3: RUN NUMBER}
Dataframe headers : PERMANENT DATUM, LOG MEASURED FROM, FEET ABOVE PERMANENT DATUM,DRILLING MEASURED FROM,KELLY BUSHING
Desired output : The values in Col2 column should be converted as a row and match the Col33 value to dataframe header and insert a row
I wrote a code to parse the file and convert to dictionary :
{'PERMANENT DATUM': 'DD', 'LOG MEASURED FROM': 'RT', 'FEET ABOVE PERMANENT DATUM': '98', 'DRILLING MEASURED FROM': 'RT', 'KELLY BUSHING': '100', 'GROUND LEVEL': '500', 'RUN DATE': '08/12/95', 'RUN NUMBER': '3'}
How to append the values in this dictionary to existing data frame? the keys in the dictionary matches dataframe headrs and is always a subset of dataframe headers.
{'file_name': dict_for_file}to store each file, then you can construct it all at once withpd.DataFrame.from_dictprobably usingorient='index'.reindexif you then want a certain order or fields that never appeared in any file.