English 中文(简体)
PandasData Frame Handeling Of Nested List within Json In Pandas table For subsequently Numpy Array
原标题:PandasData Frame Handeling Of Nested List Within Json In Pandas Table For Subsequent Numpy Array

因此,我与一家小型项目(im)的 block子公司合作。 目前,Im试图将json数据输入Pandas数据机,然后将这一数据仪转换成一个由第三方项目“CurseXcel”处理、但I digress。

目前遇到麻烦的是,在json数据字典清单中正确处理儿童。 能够制定哪些法典,基本上就是

def return_pandas():
    process = subprocess.run("lsblk --json -o NAME,SIZE,UUID,MOUNTPOINT,PATH,FSTYPE ".split(), capture_output=True, text=True)
    data = json.loads(process.stdout)
    return pd.json_normalize(data[ blockdevices ])

当时

json_pandas = return_pandas()

返回

  name   size  uuid mountpoint      path fstype                                           children
0  sda    25G  None       None  /dev/sda   None                                                NaN
1  sdb    25G  None       None  /dev/sdb   None  [{ name :  sdb1 ,  size :  512M ,  uuid :  008...
2  sr0  1024M  None       None  /dev/sr0   None                                                NaN

as a table. Again, what i m having trouble with is getting the children list within said json and pandas dataframe table to be potentially listed alongside regular devices or listed in a more normalized fashion within the children column comprising name size uuid etc. This would involve potentially using recursion to flatten all data or something else.

假设数据就是如此。

[[ sda   25G  None None  /dev/sda  None nan]
 [ sdb   25G  None None  /dev/sdb  None
  list([{ name :  sdb1 ,  size :  512M ,  uuid :  0087-DF28 ,  mountpoint :  /boot/efi ,  path :  /dev/sdb1 ,  fstype :  vfat }, { name :  sdb2 ,  size :  488M ,  uuid :  bd51147c-8f8c-4d3a-afde-4ebf67ae4558 ,  mountpoint :  /boot ,  path :  /dev/sdb2 ,  fstype :  ext2 }, { name :  sdb3 ,  size :  24G ,  uuid :  d07d17bf-ebbd-491a-b57c-f9d43b7e6be5 ,  mountpoint : None,  path :  /dev/sdb3 ,  fstype :  crypto_LUKS ,  children : [{ name :  sda3_crypt ,  size :  24G ,  uuid :  3NbbDi-BtQ4-PXzK-NVBO-cR2d-aLzZ-pzh0A5 ,  mountpoint : None,  path :  /dev/mapper/sda3_crypt ,  fstype :  LVM2_member ,  children : [{ name :  kali--vg-root ,  size :  23G ,  uuid :  6bc26692-9b22-4d38-b1c0-53e99326e9d5 ,  mountpoint :  / ,  path :  /dev/mapper/kali--vg-root ,  fstype :  ext4 }, { name :  kali--vg-swap_1 ,  size :  980M ,  uuid :  3eb8e4cc-56bb-4b5b-b2a8-bc7ac016df67 ,  mountpoint :  [SWAP] ,  path :  /dev/mapper/kali--vg-swap_1 ,  fstype :  swap }]}]}])]
 [ sr0   1024M  None None  /dev/sr0  None nan]]

如果在把桌子变成一个绝食阵列之后,我就能够拿到这份名单,因为这样一来就 great了不起的一些how。 损失一米,但可以肯定的是,损失只是一米。

this could involve flattening into a 1 dimensional array however i still want to maintain the ability to use numpyarrays. i ve thought about using recursion however dont know specifically how to grab a nested list without recreating the entire structure, nor a conditional statement that applies to nested list.

每项要求的数据

{ blockdevices : [{ name :  sda ,  size :  25G ,  uuid : None,  mountpoint : None,  path :  /dev/sda ,  fstype : None}, { name :  sdb ,  size :  25G ,  uuid : None,  mountpoint : None,  path :  /dev/sdb ,  fstype : None,  children : [{ name :  sdb1 ,  size :  512M ,  uuid :  0087-DF28 ,  mountpoint :  /boot/efi ,  path :  /dev/sdb1 ,  fstype :  vfat }, { name :  sdb2 ,  size :  488M ,  uuid :  bd51147c-8f8c-4d3a-afde-4ebf67ae4558 ,  mountpoint :  /boot ,  path :  /dev/sdb2 ,  fstype :  ext2 }, { name :  sdb3 ,  size :  24G ,  uuid :  d07d17bf-ebbd-491a-b57c-f9d43b7e6be5 ,  mountpoint : None,  path :  /dev/sdb3 ,  fstype :  crypto_LUKS ,  children : [{ name :  sda3_crypt ,  size :  24G ,  uuid :  3NbbDi-BtQ4-PXzK-NVBO-cR2d-aLzZ-pzh0A5 ,  mountpoint : None,  path :  /dev/mapper/sda3_crypt ,  fstype :  LVM2_member ,  children : [{ name :  kali--vg-root ,  size :  23G ,  uuid :  6bc26692-9b22-4d38-b1c0-53e99326e9d5 ,  mountpoint :  / ,  path :  /dev/mapper/kali--vg-root ,  fstype :  ext4 }, { name :  kali--vg-swap_1 ,  size :  980M ,  uuid :  3eb8e4cc-56bb-4b5b-b2a8-bc7ac016df67 ,  mountpoint :  [SWAP] ,  path :  /dev/mapper/kali--vg-swap_1 ,  fstype :  swap }]}]}]}, { name :  sr0 ,  size :  1024M ,  uuid : None,  mountpoint : None,  path :  /dev/sr0 ,  fstype : None}]}
                                                                                                                                                                     
最佳回答

您的预计产出吗?

法典:

df = pd.json_normalize(data=data.get("blockdevices")).explode(column="children")
df = (pd
      .concat(objs=[df, df.children.apply(func=pd.Series)], axis=1)
      .drop(columns=[0, "children"])
      .fillna("")
      .reset_index(drop=True)
      )
print(df)

结果:

  name   size uuid mountpoint      path fstype  name  size                                  uuid mountpoint       path       fstype
0  sda    25G                  /dev/sda                                                                                            
1  sdb    25G                  /dev/sdb         sdb1  512M                             0087-DF28  /boot/efi  /dev/sdb1         vfat
2  sdb    25G                  /dev/sdb         sdb2  488M  bd51147c-8f8c-4d3a-afde-4ebf67ae4558      /boot  /dev/sdb2         ext2
3  sdb    25G                  /dev/sdb         sdb3   24G  d07d17bf-ebbd-491a-b57c-f9d43b7e6be5             /dev/sdb3  crypto_LUKS
4  sr0  1024M                  /dev/sr0      

                                                                                  
问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签