Home > Net >  how to arrange list of lists of dictionaries as a table with key name as header in python
how to arrange list of lists of dictionaries as a table with key name as header in python

Time:12-06

I want to create a data frame as shown in the last section of my question. the explanation is as follows, I have this:

results
df = pd.DataFrame(results)
df

output :

results:
farm_power_output   farm_unwaked_power_output   farm_wind_direction flow_case_id    id  measurement_set_id  met_mast_results    simulation_id   timestamp         turbine_results
0   80348100.0  81027200.0  2.63545 00001WS11.6dir151.0 34875   None    [{'air_density': 1.20526, 'id': 28962, 'instan...   3zznris223wxkeimi9jrkg2n    None    [{'air_density': 1.20526, 'id': 631357, 'insta...
1   80200300.0  81226300.0  3.14159 00008WS11.7dir180.0 34876   None    [{'air_density': 1.20526, 'id': 28963, 'instan...   3zznris223wxkeimi9jrkg2n    None    [{'air_density': 1.20526, 'id': 631369, 'insta...
2   62370800.0  67676300.0  3.03687 00006WS10.1dir174.0 34877   None    [{'air_density': 1.20526, 'id': 28964, 'instan...   3zznris223wxkeimi9jrkg2n    None    [{'air_density': 1.20526, 'id': 631381, 'insta...
3   40375100.0  43515300.0  2.56563 00000WS8.6dir147.0  34878   None    [{'air_density': 1.20526, 'id': 28965, 'instan...   3zznris223wxkeimi9jrkg2n    None    [{'air_density': 1.20526, 'id': 631393, 'insta...
4   79935100.0  81509900.0  2.82743 00004WS12.4dir162.0 34879   None    [{'air_density': 1.20526, 'id': 28966, 'instan...   3zznris223wxkeimi9jrkg2n    None    [{'air_density': 1.20526, 'id': 631405, 'insta...
5   73799100.0  77291300.0  3.10669 00007WS10.8dir178.0 34880   None    [{'air_density': 1.20526, 'id': 28967, 'instan...   3zznris223wxkeimi9jrkg2n    None    [{'air_density': 1.20526, 'id': 631417, 'insta...
6   81055200.0  81348000.0  2.70526 00003WS12.0dir155.0 34881   None    [{'air_density': 1.20526, 'id': 28968, 'instan...   3zznris223wxkeimi9jrkg2n    None    [{'air_density': 1.20526, 'id': 631429, 'insta...
7   54173400.0  66155700.0  2.89725 00005WS10.0dir166.0 34882   None    [{'air_density': 1.20526, 'id': 28969, 'instan...   3zznris223wxkeimi9jrkg2n    None    [{'air_density': 1.20526, 'id': 631441, 'insta...
8   71949100.0  74568800.0  2.67035 00002WS10.6dir153.0 34883   None    [{'air_density': 1.20526, 'id': 28970, 'instan...   3zznris223wxkeimi9jrkg2n    None    [{'air_density': 1.20526, 'id': 631453, 'insta...

I did this :

df1 = df.turbine_results
df1

I got this,

df1 : 
0    [{'air_density': 1.20526, 'id': 631357, 'insta...
1    [{'air_density': 1.20526, 'id': 631369, 'insta...
2    [{'air_density': 1.20526, 'id': 631381, 'insta...
3    [{'air_density': 1.20526, 'id': 631393, 'insta...
4    [{'air_density': 1.20526, 'id': 631405, 'insta...
5    [{'air_density': 1.20526, 'id': 631417, 'insta...
6    [{'air_density': 1.20526, 'id': 631429, 'insta...
7    [{'air_density': 1.20526, 'id': 631441, 'insta...
8    [{'air_density': 1.20526, 'id': 631453, 'insta...
Name: turbine_results, dtype: object

But I want this,

turbine_results : 
air_density     id    instance_id        power    rotor_wind_speed  turbulence_intensity    unwked_power    wake_speed_factor
1.20526       631357        3         6752270.0   11.5226         0.1                   6752270.0       1.0
1.20526       631358        6         6751830.0   11.5196         0.2                   6752270.0       0.999739
1.20526       631369        3         6768860.0   11.6919         0.3                   6768860.0       1.0
1.20526       631370        2         6768600.0   11.6877         0.4                   6768600.0       0.99645
1.20526       631381        3         5639690.0   10.0387         0.5                   5639690.0       1.0
1.20526       631382        2         5658480.0   10.0382         0.6                   5658480.0       0.999954
1.20526       631393        3         3626270.0   8.53487         0.7                   3626270.0       1.0
1.20526       631394        6         3628910.0   8.53459         0.8                   3628910.0       0.99967
1.20526       631405        3         6792490.0   12.3093         0.9                   6792490.0       1.0

CodePudding user response:

You should be able to create your dataframe using the first list containing dicts and then append to the already existing dataframe using the remaining list of dicts

for i, lst in enumerate(turbine_results):
    if i == 0:
        df = pd.DataFrame(lst)
    else:
        df = df.append(lst, ignore_index=True, sort=False)

CodePudding user response:

>>> pd.DataFrame([{'a': 1, 'b': 2}, {'a': -1, 'b': 0}])
   a  b
0  1  2
1 -1  0

You can just do this with your data. Good luck!

In your case, perhaps pd.DataFrame([d for d in row for row in dataset]) since you seem to have lists of lists of dicts.

  • Related