#!/usr/bin/env python3 from pandas import read_csv from pprint import pprint df = read_csv("./csv/2.csv") pprint(list(enumerate(df.columns))) cols = [ ('chemical_db_id', 1), ('library', 2), ('name', 4), ('formula', 6), ('mass', 7), ('pubchem_cid', 10), ('pubmed_refcount', 8), ('standard_class', 11), ('inchikey', 13), ('inchikey14', 14), ('final_mz', 17), ('final_rt', 18), ('final_adduct', 19), ('adduct', 20), ('detected_adducts', 22), ('adduct_calc_mz', 23), ('msms_detected', 26), ('msms_purity', 28) ] data = [{x[0]: row[x[1]] for x in cols} for row in df.to_dict('tight')['data']] for x in data: if x['msms_detected'] == 'No': del x['msms_detected']