94 lines
2.7 KiB
Python
94 lines
2.7 KiB
Python
from pycen import con_gn
|
|
import numpy as np
|
|
import pandas as pd
|
|
import os
|
|
|
|
def get_status(lst):
|
|
sql = """
|
|
SELECT
|
|
t.cd_nom,
|
|
t.cd_ref,
|
|
t.regne,
|
|
t.phylum,
|
|
t.classe,
|
|
t.ordre,
|
|
t.famille,
|
|
t.group1_inpn,
|
|
t.group2_inpn,
|
|
t.nom_vern,
|
|
t.nom_complet,
|
|
t.nom_valide,
|
|
t.lb_nom,
|
|
--s.*
|
|
s.code_statut,
|
|
s.cd_type_statut,
|
|
s.label_statut
|
|
FROM taxonomie.v_taxref_all_listes t
|
|
JOIN taxonomie.v_bdc_status s USING (cd_nom)
|
|
WHERE t.cd_nom IN {cd_nom}
|
|
;""".format(cd_nom = tuple(lst))
|
|
return pd.read_sql_query(sql,con_gn)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
PATH = '/media/colas/Disk2/tmp/NICO'
|
|
file = 'MAIR_FAUNE&FLORE_PG2024_V0.xlsx'
|
|
sheet = 'MAIR_FLORE'
|
|
|
|
# Liste des CD_NOM en entrée
|
|
taxlist = pd.read_excel(os.path.join(PATH,file),sheet,usecols=['cd_nom'],header=1)
|
|
df = get_status(taxlist.cd_nom.astype(str))
|
|
|
|
|
|
# df.to_csv('/media/colas/SRV/FICHIERS/TRANSFERTS-EQUIPE/LC/BOCA_CD_NOM_STATUS.csv')
|
|
|
|
pivot = pd.pivot_table(
|
|
df,
|
|
values='code_statut',
|
|
index=['cd_nom', 'cd_ref','lb_nom'#,'niveau_admin','lb_adm_tr'
|
|
],
|
|
columns=['cd_type_statut'],
|
|
aggfunc=list,fill_value=None)
|
|
|
|
for c in pivot.columns:
|
|
pivot[c] = [x[0] if x is not np.NaN and len(x)==1 else x for x in pivot[c]]
|
|
if 'DH' in pivot.columns:
|
|
pivot['DH'] = [','.join(x) if (x is not np.NaN) and (len(x)==2) else x for x in pivot['DH']]
|
|
pivot.DH.replace({'CDH':''},regex=True,inplace=True)
|
|
|
|
|
|
pivlib = pd.pivot_table(
|
|
df,
|
|
values='label_statut',
|
|
index=['cd_nom', 'cd_ref','lb_nom'#,'niveau_admin','lb_adm_tr'
|
|
],
|
|
columns=['cd_type_statut'],
|
|
aggfunc=list,fill_value=None)
|
|
for c in pivlib.columns:
|
|
pivlib[c] = [x[0] if x is not np.NaN and len(x)==1 else x for x in pivlib[c]]
|
|
if 'DH' in pivot.columns:
|
|
pivlib['DH'] = [','.join(x) if (x is not np.NaN) and (len(x)==2) else x for x in pivlib['DH']]
|
|
pivlib.DH.replace({'CDH':''},regex=True,inplace=True)
|
|
|
|
|
|
print('INIT writer')
|
|
NAME_OUT = os.path.join(PATH,sheet+'_status.xlsx')
|
|
with pd.ExcelWriter(NAME_OUT) as writer:
|
|
df.to_excel(
|
|
writer,sheet_name='v_bdc_status'
|
|
)
|
|
# writer.save()
|
|
print('v_bdc_status OK !')
|
|
pivot.to_excel(
|
|
writer,sheet_name='pivot_table'
|
|
)
|
|
# writer.save()
|
|
print('pivot_table OK !')
|
|
pivlib.to_excel(
|
|
writer,sheet_name='pivot_libel'
|
|
)
|
|
# writer.save()
|
|
print('pivot_libel OK !')
|
|
|