aksummer
aksummer

Reputation: 11

Python dictionary to pandas DataFrame conversion

I have a python dictionary:

{'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:

Example DataFrame

but I would like the 'L1Data' data to be broken out into individual columns like the rest of the pandas DataFrame. How do I dot that?

Thanks for the help

Upvotes: 1

Views: 46

Answers (2)

piRSquared
piRSquared

Reputation: 294218

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

Upvotes: 1

Nawra C
Nawra C

Reputation: 166

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

Upvotes: 0

Related Questions