I have a question regarding how the following segment of code runs (i.e. the code is copied over from a response Andy Hayden gave on this question: "How do convert a pandas/dataframe to XML?")
def to_xml(df, filename=None, mode='w'):
def row_to_xml(row):
xml = ['<item>']
for i, col_name in enumerate(row.index):
xml.append(' <field name="{0}">{1}</field>'.format(col_name, row.iloc[i]))
xml.append('</item>')
return '\n'.join(xml)
res = '\n'.join(df.apply(row_to_xml, axis=1))
if filename is None:
return res
with open(filename, mode) as f:
f.write(res)
pd.DataFrame.to_xml = to_xml
Then, assuming you have a Pandas dataframe object (df), the to_xml function can be called by running:
df.to_xml()
or
df.to_xml('foo.xml')
What I don't understand is why when calling df.to_xml am I not required to pass in the instance of my dataframe object as an argument to the function since the first parameter is not optional?
More clearly, why am I not required to call to_xml in the following ways?:
df.to_xml(df)
or
df.to_xml(df, 'foo.xml')
Thanks in advance