Ad

Getting 'True' Value Instead Of Real Value When Trying To Append In Pandas Dataframe

- 1 answer

Below is my python script.I am trying to parse an xml file and trying to store values in pandas dataframe so that I can later change it to csv file.

import os
import pandas as pd
import sys
import requests
import xml.etree.ElementTree as ET
from xml.dom import minidom

tree = ET.parse('schedule.xml')
FILENAME = sys.argv[1]
COL_NAMES = ['PODNAME', 'DC', 'Upgrade']
DF = pd.DataFrame(columns = COL_NAMES)
root = tree.getroot()

def parseXml(DF, filePath):

    xmldoc = minidom.parse(filePath)
    PODs= xmldoc.getElementsByTagName("POD")
    name=[]
    DC=[]
    deferUpgrade=[]
    for pod in PODs:
        if pod.hasAttribute("name") and pod.hasAttribute("DC") and pod.hasAttribute("deferUpgrade"):
            DF = DF.append({'PODNAME': pod.getAttribute("name"), 'DC': pod.hasAttribute("DC"), 'Upgrade': pod.hasAttribute("deferUpgrade")}, ignore_index=True)
    return(DF)


DF = parseXml(DF, FILENAME)

Below is the XML file

<Schedule xmlns="http://xmlns.xyz.com/fal/downtimeschedule/V1.0">
 <Downtime end="20181020000" id="10001197610_20181027000_201810000" mode="cold" start="20181020000"/>
 <PODS>
  <POD DC="US - Washing" deferUpgrade="true" name="ABCD" patching="Production" EndTime="20181028040000">
   <CR id="12345"/>
   <CR id="12346"/>
   <CR id="123"/>
  </POD>
  <POD DC="US - Washing" deferUpgrade="true" name="ABCD-TEST" patching="Production" EndTime="20181028040000">
   <CR id="12345"/>
   <CR id="12346"/>
   <CR id="123"/>
  </POD>
 </PODS>

when I am running my script with first argument as filename I am getting below output

     PODNAME    DC Upgrade
0       ABCD  True    True
1  ABCD-TEST  True    True

However, I expect below output.

     PODNAME    DC            Upgrade
0       ABCD  US - Washing    true
1  ABCD-TEST  US - Washing    true

Also, I tried to use concat to overcome above issue but getting below ouotput

       PODNAME              DC Upgrade
0       [ABCD]  [US - Washing]  [true]
1  [ABCD-TEST]  [US - Washing]  [true]
Ad

Answer

As Nitin and Martin replied, pasting final code here.

import os
import pandas as pd
import sys
import requests
import xml.etree.ElementTree as ET
from xml.dom import minidom

tree = ET.parse('schedule.xml')
FILENAME = sys.argv[1]
COL_NAMES = ['PODNAME', 'DC', 'Upgrade']
DF = pd.DataFrame(columns = COL_NAMES)
root = tree.getroot()

def parseXml(DF, filePath):

    xmldoc = minidom.parse(filePath)
    PODs= xmldoc.getElementsByTagName("POD")
    name=[]
    DC=[]
    deferUpgrade=[]
    for pod in PODs:
        if pod.hasAttribute("name") and pod.hasAttribute("DC") and pod.hasAttribute("deferUpgrade"):
            DF = DF.append({'PODNAME': pod.getAttribute("name"), 'DC': pod.getAttribute("DC"), 'Upgrade': pod.getAttribute("deferUpgrade")}, ignore_index=True)
    return(DF)


DF = parseXml(DF, FILENAME)
Ad
source: stackoverflow.com
Ad