简体   繁体   English

Python 字典到 pandas DataFrame 转换

[英]Python dictionary to pandas DataFrame conversion

I have a python dictionary:我有一本 python 字典:

{'data': {'count': 141,
          'telemetries': [{'date': '2021-03-01 06:01:52',
                           'totalActivePower': 0.0,
                           'dcVoltage': None,
                           'powerLimit': 100.0,
                           'totalEnergy': 7175210.0,
                           'temperature': 3.591,
                           'inverterMode': 'SLEEPING',
                           'operationMode': 0,
                           'L1Data': {'acCurrent': 0.0,
                                      'acVoltage': 242.78,
                                      'acFrequency': 59.9746,
                                      'apparentPower': 0.0,
                                      'activePower': 0.0,
                                      'reactivePower': 0.0,
                                      'cosPhi': 0.0}},
                          {'date': '2021-03-01 06:41:52',
                           'totalActivePower': 0.0,
                           'dcVoltage': 429.019,
                           'groundFaultResistance': 11000.0,
                           'powerLimit': 100.0,
                           'totalEnergy': 7175210.0,
                           'temperature': 4.60093,
                           'inverterMode': 'THROTTLED',
                           'operationMode': 0,
                           'L1Data': {'acCurrent': 0.0,
                                      'acVoltage': 241.453,
                                      'acFrequency': 59.9624,
                                      'apparentPower': 0.0,
                                      'activePower': 0.0,
                                      'reactivePower': 0.0,
                                      'cosPhi': 0.0}}]}}

that I can get into a pandas Dataframe like:我可以像这样进入 pandas Dataframe :

示例数据框

but I would like the 'L1Data' data to be broken out into individual columns like the rest of the pandas DataFrame.但我希望将“L1Data”数据分解为单独的列,例如 pandas DataFrame 的 rest。 How do I dot that?我该怎么点呢?

Thanks for the help谢谢您的帮助

pd.json_normalize

pd.json_normalize(dat['data']['telemetries'])

                  date  totalActivePower  dcVoltage  powerLimit  totalEnergy  temperature inverterMode  operationMode  L1Data.acCurrent  L1Data.acVoltage  L1Data.acFrequency  L1Data.apparentPower  L1Data.activePower  L1Data.reactivePower  L1Data.cosPhi  groundFaultResistance
0  2021-03-01 06:01:52               0.0        NaN       100.0    7175210.0      3.59100     SLEEPING              0               0.0           242.780             59.9746                   0.0                 0.0                   0.0            0.0                    NaN
1  2021-03-01 06:41:52               0.0    429.019       100.0    7175210.0      4.60093    THROTTLED              0               0.0           241.453             59.9624                   0.0                 0.0                   0.0            0.0                11000.0

Try: df.L1Data.str.split(",", expand=True)试试: df.L1Data.str.split(",", expand=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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