I’m currently working in a project where I have to scrape a web page extracting data from tables. My goal is to save this information and apply some transformation to represent this data in a manner that my boss can understand it.
Right know I’m able to extract the information and save it in a nested dictionary (1) like this
dicionario =
{
'38609-2023':
[
{'Folio': '6', 'Doc.': 'Descargar Documento', 'Anexo': '2023', 'Año': '20/03/2023', 'Fecha Trámite': 'Resolución', 'Trámite': 'DESE CUENTA', 'Des. Trámite': '210337-2023', 'Correlativo': 'Cuenta (Secretaria)', 'Salas': 'Bloqueado'},
{'Folio': '5', 'Doc.': 'Descargar Documento', 'Anexo': '2023', 'Año': '17/03/2023', 'Fecha Trámite': 'Otro Tramite', 'Trámite': 'CERT. INHABILIDAD ART. 195 C.O.T', 'Des. Trámite': '209274-2023', 'Correlativo': 'Unidad Ingreso', 'Salas': 'Bloqueado'},
{'Folio': '4', 'Doc.': 'Descargar Documento', 'Anexo': '2023', 'Año': '17/03/2023', 'Fecha Trámite': 'Otro Tramite', 'Trámite': 'CERT. INHABILIDAD ART. 196 C.O.T', 'Des. Trámite': '209273-2023', 'Correlativo': 'Unidad Ingreso', 'Salas': 'Bloqueado'},
{'Folio': '2', 'Doc.': 'Descargar Documento', 'Anexo': '2023', 'Año': '17/03/2023', 'Fecha Trámite': 'Otro Tramite', 'Trámite': 'CERTIFICADO DE INGRESO', 'Des. Trámite': '209272-2023', 'Correlativo': 'Unidad Ingreso', 'Salas': 'Bloqueado'}
],
'38451-2023':
[
{'Folio': '5', 'Doc.': 'Descargar Documento', 'Anexo': '2023', 'Año': '20/03/2023', 'Fecha Trámite': 'Resolución', 'Trámite': 'DESE CUENTA', 'Des. Trámite': '210447-2023', 'Correlativo': 'Cuenta (Secretaria)', 'Salas': 'Bloqueado'},
{'Folio': '4', 'Doc.': 'Descargar Documento', 'Anexo': '2023', 'Año': '17/03/2023', 'Fecha Trámite': 'Otro Tramite', 'Trámite': 'CERT. INHABILIDAD ART. 195 C.O.T', 'Des. Trámite': '208820-2023', 'Correlativo': 'Unidad Ingreso', 'Salas': 'Bloqueado'},
{'Folio': '2', 'Doc.': 'Descargar Documento', 'Anexo': '2023', 'Año': '17/03/2023', 'Fecha Trámite': 'Otro Tramite', 'Trámite': 'CERTIFICADO DE INGRESO', 'Des. Trámite': '208819-2023', 'Correlativo': 'Unidad Ingreso', 'Salas': 'Bloqueado'}
]}
So with this i want to add a new inner key using the date key value 'Año' ('Año': '20/03/2023' , 'Año': '17/03/2023', etc) that is present in all the data.
To acomplish this i use this lines of code in the first dictionary (1):
new_dict={}
for keys, values in dicionario.items():
if not keys in new_dict:
new_dict[keys]={}
for value in values:
print(value['Año'], value, keys)
try:
new_dict[keys][value['Año']]
except KeyError:
new_dict[keys][value['Año']] = []
new_dict[keys][value['Año']].append(value)
print(new_dict)
And the result will be:
{
'38609-2023':
{
'20/03/2023': [
{'Folio': '6', 'Doc.': 'Descargar Documento', 'Anexo': '2023', 'Año': '20/03/2023', 'Fecha Trámite': 'Resolución', 'Trámite': 'DESE CUENTA', 'Des. Trámite': '210337-2023', 'Correlativo': 'Cuenta (Secretaria)', 'Salas': 'Bloqueado'
}
],
'17/03/2023': [
{'Folio': '5', 'Doc.': 'Descargar Documento', 'Anexo': '2023', 'Año': '17/03/2023', 'Fecha Trámite': 'Otro Tramite', 'Trámite': 'CERT. INHABILIDAD ART. 195 C.O.T', 'Des. Trámite': '209274-2023', 'Correlativo': 'Unidad Ingreso', 'Salas': 'Bloqueado'},
{'Folio': '4', 'Doc.': 'Descargar Documento', 'Anexo': '2023', 'Año': '17/03/2023', 'Fecha Trámite': 'Otro Tramite', 'Trámite': 'CERT. INHABILIDAD ART. 196 C.O.T', 'Des. Trámite': '209273-2023', 'Correlativo': 'Unidad Ingreso', 'Salas': 'Bloqueado'},
{'Folio': '2', 'Doc.': 'Descargar Documento', 'Anexo': '2023', 'Año': '17/03/2023', 'Fecha Trámite': 'Otro Tramite', 'Trámite': 'CERTIFICADO DE INGRESO', 'Des. Trámite': '209272-2023', 'Correlativo': 'Unidad Ingreso', 'Salas': 'Bloqueado'}
]
},
'38451-2023':
{
'20/03/2023': [
{'Folio': '5', 'Doc.': 'Descargar Documento', 'Anexo': '2023', 'Año': '20/03/2023', 'Fecha Trámite': 'Resolución', 'Trámite': 'DESE CUENTA', 'Des. Trámite': '210447-2023', 'Correlativo': 'Cuenta (Secretaria)', 'Salas': 'Bloqueado'}
],
'17/03/2023': [
{'Folio': '4', 'Doc.': 'Descargar Documento', 'Anexo': '2023', 'Año': '17/03/2023', 'Fecha Trámite': 'Otro Tramite', 'Trámite': 'CERT. INHABILIDAD ART. 195 C.O.T', 'Des. Trámite': '208820-2023', 'Correlativo': 'Unidad Ingreso', 'Salas': 'Bloqueado'},
{'Folio': '2', 'Doc.': 'Descargar Documento', 'Anexo': '2023', 'Año': '17/03/2023', 'Fecha Trámite': 'Otro Tramite', 'Trámite': 'CERTIFICADO DE INGRESO', 'Des. Trámite': '208819-2023', 'Correlativo': 'Unidad Ingreso', 'Salas': 'Bloqueado'}]}}
After this i want to flatten the nested dictionary to make it more human readable
df = pd.DataFrame.from_dict(new_dict, orient="index").stack().to_frame()
final_df = pd.DataFrame(df[0].values.tolist(), index=df.index)
print(final_df)
The result is a dataframe like this
This is not bad at all... but i want to use the keys from rows to use it as columns and use the values to fill every new column
Columns 0 replace with : Folio Doc. Anexo Año Fecha Trámite Trámite Des. Trámite Correlativo Salas Estado
Columns 1 replace with : Folio Doc. Anexo Año Fecha Trámite Trámite Des. Trámite Correlativo Salas Estado
Columns 2 replace with : Folio Doc. Anexo Año Fecha Trámite Trámite Des. Trámite Correlativo Salas Estado
I tried to explain better with a little example
I tried using pd.Dataframe.from_dict as you can see in the post but it didint work. I also tried converting the nested dictionary to json and then use pd.json_normalize but i cant get what i want. Also using
Any idea what i can do?... Maybe make a lambda funcion in all the columns? i dont know really... i dont have to much expirencie. Help me please