来自Dict的Panda Dataframe具有不同的长度值 [英] Panda Dataframe from Dict with different length value

查看:39
本文介绍了来自Dict的Panda Dataframe具有不同的长度值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的字典中创建一个数据框.该键的每个值实际上是其中包含多个值的数组.

I want to create a dataframe from my dictionary. Each value for the key is actually a array with multiple value in it.

>>> my_dict = {"a": [1,2,3], "b": [0], "c": [3,5] }

我希望第1列中的所有键以及第2列中的整个值数组.

I want all the keys in a column 1 and entire value array in the column 2.

我已经尝试过这篇文章.从条目长度不同的字典中创建数据框

I've tried this post. Creating dataframe from a dictionary where entries have different lengths

但是此解决方案将键中的所有值分隔为多列.

But this solution separates all the value from the key to multiple columns.

期望的DF应该看起来像这样

Expected DF should look like this

>>> df 
      Key_Column   Value_Column
          a            [1,2,3]
          b            [0]
          c            [3,5]

推荐答案

Series

pd.Series(my_dict).rename_axis('Key_Column').reset_index(name='Value_Column')

  Key_Column Value_Column
0          a    [1, 2, 3]
1          b          [0]
2          c       [3, 5]

这篇关于来自Dict的Panda Dataframe具有不同的长度值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆