Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Data Science by (18.4k points)

This is the loop I am using.

lines = []

for dicom in mydicoms:

    dcm1 = pydicom.dcmread(dicom)

    identity = dcm1.PatientID

    age = dcm1.PatientAge

    sex = dcm1.PatientSex

    modality = dcm1.Modality

    finding = dcm1.StudyDescription

    rows = dcm1.Rows

    columns = dcm1.Columns

    new_row = {'id':identity, 'age':age, 'sex':sex, 'modality':modality, 'finding':finding, 'rows':rows, 'columns':columns}

    df.append(new_row, ignore_index = True)

    lines.append(new_row)

When I check the data frame later it is empty.

When I take a part of the code and run it in another cell of jupyter:

df.append(new_row, ignore_index=True)

df.append(lines, ignore_index = True)

It works correctly but inside the loop its not working why?

1 Answer

0 votes
by (36.8k points)

You need to assign it back to the data frame variable.

the code for correction:

lines = []

for dicom in mydicoms:

    dcm1 = pydicom.dcmread(dicom)

    identity = dcm1.PatientID

    age = dcm1.PatientAge

    sex = dcm1.PatientSex

    modality = dcm1.Modality

    finding = dcm1.StudyDescription

    rows = dcm1.Rows

    columns = dcm1.Columns

    new_row = {'id':identity, 'age':age, 'sex':sex, 'modality':modality, 'finding':finding, 'rows':rows, 'columns':columns}

    df = df.append(new_row, ignore_index = True)

    lines.append(new_row)

Learn Python for Data Science Course to improve your technical knowledge. 

Related questions

Browse Categories

...