influxdata / influxdb-client-python

InfluxDB 2.0 python client
https://influxdb-client.readthedocs.io/en/stable/
MIT License
724 stars 187 forks source link

How to apply pivot function? #539

Closed tungrix closed 1 year ago

tungrix commented 1 year ago

I am inserting trading data of financial market using influxb-client, one of my data are something look like this, the rest are same:

{'topic': 'trade.BTCUSDT', 'data': [{'symbol': 'BTCUSDT', 'tick_direction': 'ZeroMinusTick', 'price': '16573.50', 'size': 0.002, 'timestamp': '2022-12-20T01:36:37.000Z', 'trade_time_ms': '1671500197919', 'side': 'Sell', 'trade_id': 'e5a0d095-ab09-5d70-b6c7-7edebd15cda4', 'is_block_trade': 'false'}, 
{'symbol': 'BTCUSDT', 'tick_direction': 'ZeroMinusTick', 'price': '16573.50', 'size': 0.011, 'timestamp': '2022-12-20T01:36:37.000Z', 'trade_time_ms': '1671500197925', 'side': 'Sell', 'trade_id': '6603b1be-75f2-5a81-9624-018943b2056a', 'is_block_trade': 'false'}]}

And I tried this at first

write_api.write(bucket="test5",
                            record=trade,
                            record_measurement_name="tradeBTCUSDT",
                            record_time_key="timestamp",
                            record_tag_keys=["tick_direction", "side", "is_block_trade"],
                            record_field_keys=["price", "size"])

And found that for each field keys it generates one row for me, so there are total of two rows of each data(dictionary above), I want something like each row contains all field keys(tranditional rows and columns table mapping) and it seems that pivot() function can help with it, may I ask how can I use pivot() function using influxdb-client? Any sugestion on the function/code that I should use for my case or inserting a list of dictionary? Thanks!

bednar commented 1 year ago

Hi @tungrix,

thanks for using our client.

You can use the pivot function in your query. Here is an example with pivot and querying into Pandas DataFrame:

https://github.com/influxdata/influxdb-client-python/blob/20c867d6516511fe73b0cae36c1705131cfa92f7/examples/query.py#L107

Regards