简体   繁体   English

如何使用不同的数组长度将 from_dict 提取到 pandas dataframe?

[英]How to extract from_dict to pandas dataframe with varying array length?

I have a dict, I need to convert to pandas dataframe, dict have arrays, if the arrays are of same length it is working fine, but different length array throwing valueError, second question is I need to access only few key value pairs from the dict I have a dict, I need to convert to pandas dataframe, dict have arrays, if the arrays are of same length it is working fine, but different length array throwing valueError, second question is I need to access only few key value pairs from the听写

This case working, as expected I get two rows这种情况下工作,正如预期的那样,我得到两行

my_dict = {
          "ColA" : "No",
          "ColB" : [
            {
              "ColB_a" : "2011-10-26T00:00:00Z",
              "ColB_b" : 8.3
            },
            {
              "ColB_a" : "2013-10-26T00:00:00Z",
              "ColB_b" : 5.3
            }
          ],
          "ColC" : "Graduate",
          "ColD" : [
            {
              "ColD_a" : 5436.0,
              "ColD_b" : "RD"
            },
            {
              "ColD_a" : 4658.0,
              "ColD_b" : "DV"
            }
          ],
          "ColE" : "Work"
        }

sa = pd.DataFrame(my_dict)

在此处输入图像描述

In this case ColB has only one value在这种情况下,ColB 只有一个值

my_dict = {
          "ColA" : "No",
          "ColB" : [
            {
              "ColB_a" : "2011-10-26T00:00:00Z",
              "ColB_b" : 8.3
            }
          ],
          "ColC" : "Graduate",
          "ColD" : [
            {
              "ColD_a" : 5436.0,
              "ColD_b" : "RD"
            },
            {
              "ColD_a" : 4658.0,
              "ColD_b" : "DV"
            }
          ],
          "ColE" : "Work"
        }

sa = pd.DataFrame(my_dict)

so this throws ValueError: arrays must all be same length, How this can be fixed?所以这会抛出 ValueError: arrays must all be the same length,如何解决? expected output is预计 output 是

在此处输入图像描述

I can do我可以

sa = pd.DataFrame.from_dict(my_dict, orient='index').transpose()

But I have to melt and join again.但我必须融化并再次加入。 Second Question, if I need to choose only ColA, ColB from dict to create dataframe, How this to be done?第二个问题,如果我只需要从dict中选择ColA,ColB来创建dataframe,该怎么做?

for your second question, you could select a couple of columns from your dictionary using 'columns' parameter: For example: sa = pd.DataFrame(my_dict, columns = ['ColA', 'ColD'])对于您的第二个问题,您可以使用 'columns' 参数 select 字典中的几列:例如: sa = pd.DataFrame(my_dict, columns = ['ColA', 'ColD'])

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

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