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

pd.dataframe saving only one line

Hi i’m wondering what should i do to save all those values in a dataframe…

for mask in range (len(predicted_masks)):
  folha = np.where(predicted_masks [mask,:,:] == 1 , 1, 0)
  soma_folha = np.sum(folha)
  sintoma = np.where(predicted_masks [mask,:,:] == 2 , 1, 0)
  soma_sintoma = np.sum(sintoma)
  fundo = np.where(predicted_masks [mask,:,:] == 0 , 1, 0)
  soma_fundo = np.sum(fundo)
  #print(soma_fundo, soma_folha, soma_sintoma)
  severidade = (soma_sintoma/(soma_folha+soma_sintoma))*100
  severidade = round(severidade,2)
  print(soma_fundo, soma_folha, soma_sintoma, severidade)

  d = {'mask': mask, 'soma_folha':soma_folha, 'soma_sintoma':soma_sintoma, 'soma_fundo':soma_fundo, 'severidade': severidade}
  df = pd.DataFrame([d])
  df.to_csv('/content/drive/MyDrive/DB_mosca_minadora/pred_csv/pred_test_db_anotated.csv', index=False)

already tried to save each one separately but it wont came up..

i needed to save all printed values in a dataframe, thats for 304 images (304 lines) buts it only saves the last line

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

enter image description here

can someone help me?

>Solution :

You are overwriting and saving your dataframe within the loop. You should instead do something like the following:

df = pd.DataFrame(columns=['mask', 'soma_folha', 'soma_sintoma', 'soma_fundo', 'severidade'])

for mask in range (len(predicted_masks)):
  folha = np.where(predicted_masks [mask,:,:] == 1 , 1, 0)
  soma_folha = np.sum(folha)
  sintoma = np.where(predicted_masks [mask,:,:] == 2 , 1, 0)
  soma_sintoma = np.sum(sintoma)
  fundo = np.where(predicted_masks [mask,:,:] == 0 , 1, 0)
  soma_fundo = np.sum(fundo)
  #print(soma_fundo, soma_folha, soma_sintoma)
  severidade = (soma_sintoma/(soma_folha+soma_sintoma))*100
  severidade = round(severidade,2)
  print(soma_fundo, soma_folha, soma_sintoma, severidade)

  d = {'mask': mask, 'soma_folha':soma_folha, 'soma_sintoma':soma_sintoma, 'soma_fundo':soma_fundo, 'severidade': severidade}
  new_df = pd.DataFrame([d])
  df = pd.concat([df, new_df])
df.to_csv('/content/drive/MyDrive/DB_mosca_minadora/pred_csv/pred_test_db_anotated.csv', index=False)
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