Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert ceilometer output to python dataframe

I'm a little new to Python and openstack ceilometer. I'm reading ceilometer data using the following code:

import ceilometerclient.client

cclient = ceilometerclient.client.get_client(2, os_username="Ceilometeradmin", os_password="blahblah", os_tenant_name="blahblah", os_auth_url="http://xxx.xx.xx.x:5000/v2.0")

query = [dict(field='resource_id', op='eq', value='dd893564-85e5-43f8-a384-086417f1d82c')]

ls = cclient.meters.list(q=query)

(Please see picture of output attached)

enter image description here

Does anyone know how i could convert this into a dataframe?

I tried : ls2 = pandas.DataFrame(ls, columns=["user_id", "name", "resource_id", "source", "meter_id", "project_id", "type", "unit"])

but get the following error:

Traceback (most recent call last):
File "", line 1, in File "/usr/lib/python2.7/dist-packages/pandas/core/frame.py", line 250, in init copy=copy)
File "/usr/lib/python2.7/dist-packages/pandas/core/frame.py", line 363, in _init_ndarray return create_block_manager_from_blocks([values.T], [columns, index])
File "/usr/lib/python2.7/dist-packages/pandas/core/internals.py", line 3750, in create_block_manager_from_blocks construction_error(tot_items, blocks[0].shape[1:], axes, e)
File "/usr/lib/python2.7/dist-packages/pandas/core/internals.py", line 3732, in construction_error passed,implied))
ValueError: Shape of passed values is (1, 2), indices imply (8, 2)

if someone could help it would really be much much appreciated..

Thank you

Best Wishes T

like image 261
tezzaaa Avatar asked Mar 24 '26 03:03

tezzaaa


1 Answers

With the help of a colleague we managed to find the solution. I'm posting it in case it's useful to anyone trying to convert ceilometer data into a dataframe.

ls2 = str(ls)[8:-2]
tmp = ls2.replace("u'","'")
tmp = tmp.replace("<Meter","")
tmp = tmp.replace("}>","}")
tmp= "["+tmp+"]"
tmp = tmp.replace("'", "\"")
parsed = json.loads(tmp)
ls3 = pandas.DataFrame(parsed)

Replaced characters which make it as invalid dictionary and converted into dataframe

like image 147
tezzaaa Avatar answered Mar 25 '26 17:03

tezzaaa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!