Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
66 lines (46 sloc) 2.04 KB
#!/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.