Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can i add data from a dataframe to a dictionary using For

Im trying to add all data from my dataframe "tabela_proje" to a dictionary

the data frame is like this

    NOME       DESCRIÇÃO
0   PRO-NO-FEN  FENDI
1   FORMAÇÃO    NaN
2   PRO-NO-170  NaN
3   PRO-NVS-SCY STADE CHARLETY

i tried using a for loop:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for i,j in tabela_proje.iterrows():
 dados = [j.to_dict()]

Im recieving this:

[{'NOME': 'PRO-NO-CBD', 'DESCRIÇÃO': nan}]

But instead of getting all data from the dataframe i only get the last row of data from the dataframe, can anyone tell what´s wrong please?!

>Solution :

IIUC, there is no need to loop here, just use a list-oriented to_dict :

out = df.to_dict(orient="list") #orient="dict" by default

Output :

print(out)

{'NOME': ['PRO-NO-FEN', 'FORMAÇÃO', 'PRO-NO-170', 'PRO-NVS-SCY'],
 'DESCRIÇÃO': ['FENDI', nan, nan, 'STADE CHARLETY']}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading