来自数组=>的Pandas Multiindex; TypeError:无法散列的类型:'dict' [英] Pandas Multiindex from array => TypeError: unhashable type: 'dict'

查看:88
本文介绍了来自数组=>的Pandas Multiindex; TypeError:无法散列的类型:'dict'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过具有以下结构的数组创建数据框:

I'm trying to create the dataframe from the array with following structure:

df = [[{'date_time': Timestamp('2015-05-22 05:37:59'),
        'name': 'Tom',
        'value': '129'},
       {'date_time': Timestamp('2015-05-22 05:37:59'),
        'name': 'Kate',
        'value': '0'},
       {'date_time': Timestamp('2015-05-22 05:37:59'),
        'name': 'GroupeId',
        'value': '0'}, {...}, {...}, {...}],[another list of dictionaries like the first one],[and another one]]

使用此代码:

def create_from_arr():
    baby_array=pd.MultiIndex.from_tuples(df, names=['sessions', 'behaves'])
    return baby_array

我有以下错误,我无法理解:

I have the following error, that I couldn't understand:

TypeError: unhashable type: 'dict'

我想要的输出是:

list 
                   date_time      name value
 1    0 2015-05-22 05:37:59       Tom   129
      1 2015-05-22 05:37:59      Kate     0
      2 2015-05-22 05:37:59  GroupeId     0
 2    3 2015-05-26 05:56:59     Hence   129
      4 2015-05-26 05:56:59      Kate     0
      5 2015-05-26 05:56:59     Julie     0
 3    ......................    ......  ......

推荐答案

我仍然不确定您到底想对MultiIndex做什么,但这是在多级数组中拼合"字典的一种方法,将数据正确加载到数据框中:

I am still not sure what exactly you want to do with the MultiIndex, but here is one way to "flatten" your dictionary in your multi-level arrays and load your data into the dataframe properly:

已将列表"和索引"更新为MultiIndex

In [100]: data = [[{'date_time': Timestamp('2015-05-22 05:37:59'),
   .....:         'name': 'Tom',
   .....:         'value': '129'},
   .....:        {'date_time': Timestamp('2015-05-22 05:37:59'),
   .....:         'name': 'Kate',
   .....:         'value': '0'},
   .....:        {'date_time': Timestamp('2015-05-22 05:37:59'),
   .....:         'name': 'GroupeId',
   .....:         'value': '0'}], [{'date_time': Timestamp('2015-05-22 05:37:59'),
   .....:         'name': 'Tom',
   .....:         'value': '129'},
   .....:        {'date_time': Timestamp('2015-05-22 05:37:59'),
   .....:         'name': 'Kate',
   .....:         'value': '0'},
   .....:        {'date_time': Timestamp('2015-05-22 05:37:59'),
   .....:         'name': 'GroupeId',
   .....:         'value': '0'}]]

In [101]: df = pd.DataFrame(columns=['list', 'date_time', 'name', 'value'])

In [102]: for idx, each in enumerate(data, 1):
   .....:     temp = pd.DataFrame(each)
   .....:     temp['list'] = idx  # manually assign "list" index
   .....:     df = df.append(temp, ignore_index=True)
   .....:     
In [103]: df = df.reset_index()

In [104]: df.set_index(['list', 'index'])
Out[104]: 
                     date_time      name value
list index                                    
1    0     2015-05-22 05:37:59       Tom   129
     1     2015-05-22 05:37:59      Kate     0
     2     2015-05-22 05:37:59  GroupeId     0
2    3     2015-05-22 05:37:59       Tom   129
     4     2015-05-22 05:37:59      Kate     0
     5     2015-05-22 05:37:59  GroupeId     0

这篇关于来自数组=>的Pandas Multiindex; TypeError:无法散列的类型:'dict'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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