将大量数据(字典)转换为表格

Converting huge sets of data (dictionary) to tables

enter image description here我想将数据转换为表格格式。你能帮我解决这个问题吗?我在下面显示了从 json 文件中提取的两行数据。我需要以表格形式转换这些。有很多这样的行(超过 5000);其中 2 行如下所示。

{'HFProbeCounter': 1461593, 'Channel': 1, 'SeekOffset': 16, 'SelectedTool': 21, 'ActiveTool': 21, 'GCode': 'MSG("PLANEN , Tool : 001691_A")', 'IpoGC': 'G1', 'ipoReadError': None, 'laBuf': 1}

{'HFProbeCounter': 1461597, 'Channel': 1, 'SeekOffset': 20, 'SelectedTool': 21, 'ActiveTool': 21, 'GCode': 'N32 SUPA G0 Z=_Z_HOME D0', 'IpoGC': 'G0', 'ipoReadError': None, 'laBuf': 0}

我想将它们放入带有标签的列中...

columns=['HFProbeCounter', 'Channel', 'SeekOffset', 'SelectedTool', 'ActiveTool', 'GCode', 'IpoGC', 'ipoReadError', 'laBuf']

我试过使用下面的代码,但是由于数据集很大,这个代码不起作用:

from tabulate import tabulate

headers = ['HFProbeCounter', 'Channel', 'SeekOffset', 'SelectedTool', 'ActiveTool', 'GCode', 'IpoGC', 'ipoReadError', 'laBuf']
print(tabulate(data, headers=headers))

您可以使用 pandas:

data = [{'HFProbeCounter': 1461593, 'Channel': 1, 'SeekOffset': 16, 'SelectedTool': 21, 'ActiveTool': 21, 'GCode': 'MSG("PLANEN , Tool : 001691_A")', 'IpoGC': 'G1', 'ipoReadError': None, 'laBuf': 1},
        {'HFProbeCounter': 1461597, 'Channel': 1, 'SeekOffset': 20, 'SelectedTool': 21, 'ActiveTool': 21, 'GCode': 'N32 SUPA G0 Z=_Z_HOME D0', 'IpoGC': 'G0', 'ipoReadError': None, 'laBuf': 0}]

import pandas as pd
df = pd.DataFrame(data)

df

 HFProbeCounter  Channel  SeekOffset  ...  IpoGC  ipoReadError laBuf
0         1461593        1          16  ...     G1          None     1
1         1461597        1          20  ...     G0          None     0

此结构的列为:

df.columns

Index(['HFProbeCounter', 'Channel', 'SeekOffset', 'SelectedTool', 'ActiveTool',
       'GCode', 'IpoGC', 'ipoReadError', 'laBuf'],
      dtype='object')

您可以尝试使用pandas.from_dict 方法。正如他们所举例:

>>> data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']}
>>> pd.DataFrame.from_dict(data)
   col_1 col_2
0      3     a
1      2     b
2      1     c
3      0     d

像这样你可以直接将这个字典转换成一个 pandas DataFrame 对象,我可以更接近你所说的表格格式。 pandas 还获得了一个连接选项,允许您遍历多个字典,然后导致单个数据帧:

my_table = pd.concat([pd.DataFrame.from_dict(data) for data in bunch_of_dictionaries]) 

希望对您有所帮助。

这就是我处理您的问题的方式,但是我会首先将所有这些指令变成一个包含所有指令的列表。之后,您可以简单地使用 pd.DataFrame() 并传递您的列表:

import pandas as pd
data = {'HFProbeCounter': 1461593, 'Channel': 1, 'SeekOffset': 16, 'SelectedTool': 21, 'ActiveTool': 21, 'GCode': 'MSG("PLANEN , Tool : 001691_A")', 'IpoGC': 'G1', 'ipoReadError': None, 'laBuf': 1}
data_1 = {'HFProbeCounter': 1461597, 'Channel': 1, 'SeekOffset': 20, 'SelectedTool': 21, 'ActiveTool': 21, 'GCode': 'N32 SUPA G0 Z=_Z_HOME D0', 'IpoGC': 'G0', 'ipoReadError': None, 'laBuf': 0}

data_to_df = [data,data_1]
df = pd.DataFrame(data_to_df)
print(df)

输出:

   HFProbeCounter  Channel  SeekOffset  SelectedTool  ActiveTool                            GCode IpoGC ipoReadError  laBuf
0         1461593        1          16            21          21  MSG("PLANEN , Tool : 001691_A")    G1         None      1
1         1461597        1          20            21          21         N32 SUPA G0 Z=_Z_HOME D0    G0         None      0