Home > Back-end >  Python: set dictionary key values as column names of a dataframe
Python: set dictionary key values as column names of a dataframe

Time:04-07

I have a dictionary that looks like this:

('USD/EUR', {'symbol': 'USD/EUR', 'timestamp': 1649298445000, 'datetime': '2022-04-07T02:27:25.000Z', 'high': 1.43, 'low': 1.42, 'bid': 1.43, 'bidVolume': None, 'ask': 1.42, 'askVolume': None, ...})

How can I make the key names as column names in a DataFrame in one line. i.e.

df:
symbol    timestamp      datetime                  high ...
USD/EUR   1649298445000  2022-04-07T02:27:25.000Z  1.43 ...

I am able to access the dictionary but not separate key/value pairs.

CodePudding user response:

The DataFrame constructor accepts a list of dictionaries:

In [2]: _, d = tuple_given_in_question

In [3]: pd.DataFrame([d])
Out[3]:
    symbol      timestamp                  datetime  high   low   bid bidVolume   ask askVolume
0  USD/EUR  1649298445000  2022-04-07T02:27:25.000Z  1.43  1.42  1.43      None  1.42      None
  • Related