-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlci_to_bw2.py
66 lines (46 loc) · 2.04 KB
/
lci_to_bw2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 31 15:21:32 2017
@author: massimo
"""
import pandas as pd
import numpy as np
def lci_to_bw2(mydb):
'''A function to convert a pd.Dataframe to a dict
to be used as database in bw2'''
act_keys_raw = list(mydb.columns[0:5])
act_keys_bw2 = [i.replace('Activity ','') for i in act_keys_raw]
exc_keys_raw = list(mydb.columns[5:])
exc_keys_bw2 = [i.replace('Exchange ','') for i in exc_keys_raw]
def exc_to_dict(df_data, some_list):
exc_data = (pd.DataFrame(list(df_data.values), index = list(exc_keys_bw2))).T
exc_data = exc_data.dropna(axis=1, how='any') # remove columns withouth the data
e_values = (exc_data.values).tolist()[0]
e_values = [(e_values[0],e_values[1])] + e_values[2:]
some_list.append(dict(zip(list(exc_data.columns)[1:], e_values)))
def act_to_dict(act_data):
a_keys = act_keys_bw2[2:] + ['exchanges']
return dict(zip(a_keys, act_data))
def bio_to_dict(bio_data):
b_keys = act_keys_bw2[2:]
return dict(zip(b_keys, bio_data))
db_keys = []
db_values =[]
for act in mydb['Activity name'].unique():
sel = mydb[mydb['Activity name'] == act]
db_key = (list(sel['Activity database'])[0], list(sel['Activity code'])[0])
db_keys.append(db_key)
if list(sel['Activity type'].unique())[0] == 'biosphere':
my_bio_data = list(sel.iloc[0,2:5].values)
db_value = bio_to_dict(my_bio_data)
db_values.append(db_value)
else:
my_exc = []
for i in range(sel.shape[0]):
exc_to_dict(sel.iloc[i,5:],my_exc)
my_act_data = list(sel.iloc[0,2:5].values) + [my_exc]
db_value = act_to_dict(my_act_data)
db_values.append(db_value)
bw2_db = dict(zip(db_keys, db_values))
return bw2_db # We have a dict to be used as database. Perfect.