Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 185921
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 10, 20222022-06-10T11:05:54+00:00 2022-06-10T11:05:54+00:00

python – Flatten a nested xml with pandas

  • 0

[ad_1]

I have the follow xml in this format:

<?xml version='1.0' encoding='UTF-8'?>
<ettevotjad>
  <ettevotja>
    <nimi>000 Holdings OÜ</nimi>
    <ariregistri_kood>16372442</ariregistri_kood>
    <ettevotja_oiguslik_vorm>Osaühing</ettevotja_oiguslik_vorm>
    <ettevotja_oigusliku_vormi_alaliik/>
    <kmkr_nr/>
    <ettevotja_staatus>R</ettevotja_staatus>
    <ettevotja_staatus_tekstina>Registrisse kantud</ettevotja_staatus_tekstina>
    <ettevotja_esmakande_kpv>23.11.2021</ettevotja_esmakande_kpv>
    <ettevotja_aadress>
      <asukoht_ettevotja_aadressis/>
      <asukoha_ehak_kood/>
      <asukoha_ehak_tekstina></asukoha_ehak_tekstina>
      <indeks_ettevotja_aadressis/>
      <ads_adr_id></ads_adr_id>
      <ads_ads_oid></ads_ads_oid>
      <ads_normaliseeritud_taisaadress/>
    </ettevotja_aadress>
    <teabesysteemi_link>https://ariregister.rik.ee/est/company/16372442</teabesysteemi_link>
  </ettevotja>
  <ettevotja>
    <nimi>001 group OÜ</nimi>
    <ariregistri_kood>12754230</ariregistri_kood>
    <ettevotja_oiguslik_vorm>Osaühing</ettevotja_oiguslik_vorm>
    <ettevotja_oigusliku_vormi_alaliik/>
    <kmkr_nr/>
    <ettevotja_staatus>R</ettevotja_staatus>
    <ettevotja_staatus_tekstina>Registrisse kantud</ettevotja_staatus_tekstina>
    <ettevotja_esmakande_kpv>17.11.2014</ettevotja_esmakande_kpv>
    <ettevotja_aadress>
      <asukoht_ettevotja_aadressis>Õismäe tee 78-9</asukoht_ettevotja_aadressis>
      <asukoha_ehak_kood>0176</asukoha_ehak_kood>
      <asukoha_ehak_tekstina>Haabersti linnaosa, Tallinn, Harju maakond</asukoha_ehak_tekstina>
      <indeks_ettevotja_aadressis>13513</indeks_ettevotja_aadressis>
      <ads_adr_id>2182337</ads_adr_id>
      <ads_ads_oid></ads_ads_oid>
      <ads_normaliseeritud_taisaadress>Harju maakond, Tallinn, Haabersti linnaosa, Õismäe tee 78-9</ads_normaliseeritud_taisaadress>
    </ettevotja_aadress>
    <teabesysteemi_link>https://ariregister.rik.ee/est/company/12754230</teabesysteemi_link>
  </ettevotja>
</ettevotjad>

Using pandas‘ .read_xml() yields:

 import pandas as pd

 data = pd.read_xml('test_file.xml')

 print(data.head(2).to_string())
              nimi  ariregistri_kood ettevotja_oiguslik_vorm  ettevotja_oigusliku_vormi_alaliik kmkr_nr ettevotja_staatus ettevotja_staatus_tekstina ettevotja_esmakande_kpv  ettevotja_aadress                               teabesysteemi_link
0  000 Holdings OÜ          16372442                Osaühing                                NaN    None                 R         Registrisse kantud              23.11.2021                NaN  https://ariregister.rik.ee/est/company/16372442
1     001 group OÜ          12754230                Osaühing                                NaN    None                 R         Registrisse kantud              17.11.2014                NaN  https://ariregister.rik.ee/est/company/12754230

Notice in the dataframe 'ettevotja_aadress' is NaN, but in fact if you look at the xml structure, it’s nested with those sub columns/headers. How do I expand out those nested columns into the dataframe?

I thought one way to do it was to simply read in the file, remove the <ettevotja_aadress> and <ettevotja_aadress> tags, then read into pandas, but it seems like there should be direct way to do this, similar to pandas‘ .json_normalize().

[ad_2]

  • 0 0 Answers
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 401 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 371 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 367 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.