简体   繁体   中英

Converting the Nested Json format/ file into Flat files using Pandas Dataframe

I am facing one of the problem here. I have data set which is nested

 {
    "members": [
      {
        "firstname": "John", 
        "lastname": "Doe",
        "orgname": "Anon",
        "phone": "916-555-1234",
        "mobile": "",
      },
      {
        "firstname": "Jane",
        "lastname": "Doe",
        "orgname": "Anon",
        "phone": "916-555-4321",
        "mobile": "916-555-7890",
      },
    "teamname": "1",
    "team_size": "5",
    "team_status": "low"
    }

and another one which is not nested

{
"members": [
  {
    "firstname": "John", 
    "lastname": "Doe",
    "orgname": "Anon",
    "phone": "916-555-1234",
    "mobile": "",
  },
"teamname": "1",
"team_size": "5",
"team_status": "low"
}

I have handled the nested one through the code

df2 = pd.DataFrame.from_dict(json_normalize(json_file2), orient='columns')

print(df2)

df3 = pd.concat([json_normalize(x) for x in df2['members'].values.tolist()], keys= df2.index)

df3 = df2.drop('members', 1).join(df3.reset_index(level=1, drop=True)).reset_index(drop=True)

I am getting the error saying "saying “TypeError: 'float' object is not subscriptable”"

Can you please help me out with the issue.

{
"teams": [
{

"members": [
  {
    "firstname": "John", 
    "lastname": "Doe",
    "orgname": "Anon",
    "phone": "916-555-1234",
    "mobile": "",
  },
  {
    "firstname": "Jane",
    "lastname": "Doe",
    "orgname": "Anon",
    "phone": "916-555-4321",
    "mobile": "916-555-7890",
  },
"teamname": "1",
"team_size": "5",
"team_status": "low"
},
{

"members": [
  {
    "firstname": "Mickey",
    "lastname": "Moose",
    "orgname": "Moosers",
    "phone": "916-555-0000",
    "mobile": "916-555-1111",
  },
"teamname": "2",
"team_size": "5",
"team_status": "low"
]
}       
]

}

For me working:

d1 = {
    "members": [
      {
        "firstname": "John", 
        "lastname": "Doe",
        "orgname": "Anon",
        "phone": "916-555-1234",
        "mobile": "",
      },
      {
        "firstname": "Jane",
        "lastname": "Doe",
        "orgname": "Anon",
        "phone": "916-555-4321",
        "mobile": "916-555-7890",
      }],
    "teamname": "1",
    "team_size": "5",
    "team_status": "low"
    }

d2 = {
"members": [
  {
    "firstname": "John", 
    "lastname": "Doe",
    "orgname": "Anon",
    "phone": "916-555-1234",
    "mobile": "",
  }],
"teamname": "1",
"team_size": "5",
"team_status": "low"
}

df1 = json_normalize(d1, 'members', ['team_size', 'team_status','teamname'])
print (df1)
  firstname lastname        mobile orgname         phone team_size teamname  \
0      John      Doe                  Anon  916-555-1234         5        1   
1      Jane      Doe  916-555-7890    Anon  916-555-4321         5        1   

  team_status  
0         low  
1         low  

df2 = json_normalize(d2, 'members', ['team_size', 'team_status','teamname'])
print (df2)
  firstname lastname mobile orgname         phone team_size teamname  \
0      John      Doe           Anon  916-555-1234         5        1   

  team_status  
0         low  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM