简体   繁体   中英

Converting a dictionary to a dataframe in python

I have this dictionary record randomly generated looking like this (could have potentially more record)

{'Device_ID': 4, 'Temp1': 58.685912388818124, 'Temp2': 74.55450362769994, 'Temp3': 29.342956194409062, 'Temp_Ambient': 22.89076035200238, 'Time': 19}
{'Device_ID': 6, 'Temp1': 48.75315131444877, 'Temp2': 63.62846644589365, 'Temp3': 24.376575657224386, 'Temp_Ambient': 22.540969718435385, 'Time': 15}

I would like the keys: Device ID, Temp1, Temp2, Temp3, Time, Temp_Ambient to be columns of the dataframe

when i try:

df = pd.DataFrame([record], columns=record.keys())

it only shows one row of data.

Assuming it's a list of dicts:

lod = [{'Device_ID': 4, 'Temp1': 58.685912388818124, 'Temp2': 74.55450362769994, 'Temp3': 29.342956194409062, 'Temp_Ambient': 22.89076035200238, 'Time': 19},
{'Device_ID': 6, 'Temp1': 48.75315131444877, 'Temp2': 63.62846644589365, 'Temp3': 24.376575657224386, 'Temp_Ambient': 22.540969718435385, 'Time': 15}]
df = pd.DataFrame(lod)
print(df)

Output:

   Device_ID      Temp1      Temp2      Temp3  Temp_Ambient  Time
0          4  58.685912  74.554504  29.342956      22.89076    19
1          6  48.753151  63.628466  24.376576      22.54097    15

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM