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 to make a dataframe from print result in for loop

I need to print my for loop results in to a dataframe. Here is my for loop..

import os

for filename in os.listdir("/data/rrd_dump_xml/"):
    if filename.endswith(".xml") : 
        totaldir="/data/rrd_dump_xml/"+filename
     
        tree=et.parse(totaldir)
        root=tree.getroot()
        NAME = []
        for name in root.iter('name'):
            NAME.append(name.text)
        UPDATE = []
        for update in root.iter('lastupdate'):
             UPDATE.append(update.text)
        updated = datetime.datetime.fromtimestamp(int(UPDATE[0]))
        lastupdate=updated.strftime('%Y-%m-%d %H:%M:%S')
        ParaValue = []
        for parameterevalue in root.iter('value'):
            ParaValue.append(parameterevalue.text)
            
        
        print(filename,lastupdate,NAME[0],ParaValue[0])
        print(filename,lastupdate,NAME[1],ParaValue[1])
       
        
    else:
        print("Error")

I need to get an dataframe with below format of column headers..

filename lastupdate Name Value 

Note: In each file in for loop, there will be two print results( print(filename,lastupdate,NAME[0],ParaValue[0]) and print(filename,lastupdate,NAME[1],ParaValue[1]) )

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

can some one help me to do this? I checked with some examples
Writing output of a for loop to pandas data-frame but when I use those methods I am not getting correct output.

Tried sample answer.

df = pd.DataFrame(list(zip(cutoff_list , number_list)), 
           columns =['cutoff', 'number']) 

>Solution :

Instead of printing the output, add it to a list, and convert the list to a dataframe.

import os
import pandas as pd

content = []
for filename in os.listdir("/data/rrd_dump_xml/"):
    if filename.endswith(".xml") : 
        totaldir="/data/rrd_dump_xml/"+filename
     
        tree=et.parse(totaldir)
        root=tree.getroot()
        NAME = []
        for name in root.iter('name'):
            NAME.append(name.text)
        UPDATE = []
        for update in root.iter('lastupdate'):
             UPDATE.append(update.text)
        updated = datetime.datetime.fromtimestamp(int(UPDATE[0]))
        lastupdate=updated.strftime('%Y-%m-%d %H:%M:%S')
        ParaValue = []
        for parameterevalue in root.iter('value'):
            ParaValue.append(parameterevalue.text)

        # print(filename,lastupdate,NAME[0],ParaValue[0])            
        content.append({"filename": filename,
                        "lastupdate": lastupdate,
                        "Name": NAME[0],
                        "Value": ParaValue[0]})

        # print(filename,lastupdate,NAME[1],ParaValue[1])
        content.append({"filename": filename,
                        "lastupdate": lastupdate,
                        "Name": NAME[1],
                        "Value": ParaValue[1]})
       
        
    else:
        print("Error")

dataframe = pd.DataFrame(content)
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