Can we extract dictionary from pandas dataframe in a certain format

Viewed 48

trying to get dataframe value to extract in a specific format in the documentation I read about to_dict but it's not giving me exact o/p. is there any way to serialize df data efficiently.

data used to create dataframe

data = [{'customer': 'XYZ', 'company': 'aditya', 'source_scheme': 'aditya', 'target_scheme': None, 'folio_no': '100', 'units': Decimal('0.000000'), 'market_value': 70.606, 'stp_amount': Decimal('1000.000000'), 'frequency': 'M', 'stp_start_date': datetime.datetime(2011, 4, 6, 0, 0), 'stp_end_date': datetime.datetime(2016, 4, 21, 0, 0), 'stp_due_date': datetime.datetime(2016, 4, 21, 0, 0), 'stp_ref_no': '191796'}, {'customer': 'XYZ', 'company': 'aditya', 'source_scheme': 'aditya', 'target_scheme': None, 'folio_no': '100', 'units': Decimal('0.000000'), 'market_value': 70.606, 'stp_amount': Decimal('4000.000000'), 'frequency': 'M', 'stp_start_date': datetime.datetime(2015, 5, 12, 0, 0), 'stp_end_date': datetime.datetime(2040, 12, 28, 0, 0), 'stp_due_date': datetime.datetime(2040, 12, 28, 0, 0), 'stp_ref_no': '204243'}, {'customer': 'XYZ', 'company': 'aditya', 'source_scheme': 'aditya', 'target_scheme': None, 'folio_no': '100', 'units': Decimal('0.000000'), 'market_value': 70.606, 'stp_amount': Decimal('1000.000000'), 'frequency': 'M', 'stp_start_date': datetime.datetime(2011, 4, 6, 0, 0), 'stp_end_date': datetime.datetime(2016, 4, 21, 0, 0), 'stp_due_date': datetime.datetime(2016, 4, 21, 0, 0), 'stp_ref_no': '191796'}, {'customer': 'XYZ', 'company': 'aditya', 'source_scheme': 'aditya', 'target_scheme': None, 'folio_no': '100', 'units': Decimal('0.000000'), 'market_value': 70.606, 'stp_amount': Decimal('4000.000000'), 'frequency': 'M', 'stp_start_date': datetime.datetime(2015, 5, 12, 0, 0), 'stp_end_date': datetime.datetime(2040, 12, 28, 0, 0), 'stp_due_date': datetime.datetime(2040, 12, 28, 0, 0), 'stp_ref_no': '204243'}]

df = pd.DataFrame(data)
df.to_dict('records')

o/p expected

{
    'rows':[
        {
            'cells': [
                {'type': 'text', 'value': 'xyz'}, 
                {'type': 'number', 'value': 20094.62}
            ]
        }
    ]
}

in this o/p will convert each cell of dataframe rows to this format anything small is also helpful thanks in advance.

0 Answers
Related