Untitled

mail@pastecode.io avatar
unknown
python
6 months ago
1.0 kB
4
Indexable
Never
instrument_names = []
tags = []
units = []
descriptions = []
placeholder = 'N/A'

for column_name in df.columns:
    try:
        data_str = df[column_name].iloc[0]
        if data_str is not None:
            data_dict = eval(data_str)  
            tag_key = list(data_dict.keys())[0]
            tag_data = data_dict.get(tag_key, {})
            units_value = tag_data.get('units', placeholder)
            description_value = tag_data.get('description', placeholder)
        else:
            tag_key = placeholder
            units_value = placeholder
            description_value = placeholder
    except (SyntaxError, TypeError, IndexError):
        tag_key = placeholder
        units_value = placeholder
        description_value = placeholder
    instrument_names.append(column_name)
    tags.append(tag_key)
    units.append(units_value)
    descriptions.append(description_value)

result_df = pd.DataFrame({
    'Instrument Name': instrument_names,
    'Tag': tags,
    'Units': units,
    'Description': descriptions
})