简体   繁体   English

使用Pandas Dataframe将Nested Json格式/文件转换为平面文件

[英]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') df2 = pd.DataFrame.from_dict(json_normalize(json_file2),orient ='columns')

print(df2) 打印(df2)

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

df3 = df2.drop('members', 1).join(df3.reset_index(level=1, drop=True)).reset_index(drop=True) 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”" 我收到错误消息,说“说“ TypeError:'float'对象不可下标””

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  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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