以元组为键将Dictionary转换为Dataframe [英] Converting Dictionary to Dataframe with tuple as key

查看:116
本文介绍了以元组为键将Dictionary转换为Dataframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的字典

df_dict = {(7, 'hello'): {1}, (1, 'fox'): {2}}

我想将其转换为一个数据帧,其中元组的第一部分是行标题,而元组的第二部分是列标题.我尝试过:

I want to transform it into a dataframe where the first part of the tuple is the row header, and the second part of the tuple is the column header. I tried this:

doc_df = pd.DataFrame(df_dict, index=[df_dict.keys()[0]], columns = [df_dict.keys()[1]])

但是我得到了错误TypeError: 'dict_keys' object does not support indexing

我希望我的数据框看起来像这样:

I want my dataframe to look like:

_ | fox  | hello  
1 | 2    | null  
7 | null | 1

如何索引键?

推荐答案

获得TypeError的原因是df_dict.keys()是一个迭代器,它从dict一次生成键.它产生的元素将是(7, 'hello')(1, 'fox'),但是它并不事先"知道.迭代器本身不知道它有多少个元素或这些元素可能具有什么样的结构,特别是它没有任何方法可以通过索引号访问元素.

The reason you're getting the TypeError is that df_dict.keys() is an iterator which yields keys from the dict one by one. The elements it yields will be (7, 'hello') and (1, 'fox'), but it doesn't "know" that in advance. The iterator itself doesn't have any idea how many elements it has or what sort of structure those elements might have, and in particular, it doesn't have any way to access an element by index number.

现在,您可以使用itertools.islice函数从可迭代对象中访问给定编号的元素,但这涉及到丢弃事先出现的所有内容.所以那不是你想要的.

Now, you can use the itertools.islice function to access a given-numbered element from an iterable, but it involves throwing away everything that comes beforehand. So that's not what you want.

您正在询问的问题的答案(即您如何索引键)是首先将它们转换为列表:

The answer to the question you're asking, which is how you index into the keys, is to convert them into a list first:

l = list(df_dict.keys())

,然后可以使用l[0]l[1]等.

但是,这实际上并不是您的应用程序真正需要的.在您的示例中,结果列表将是

But even that isn't what you're actually going to need for your application. The resulting list, in your example, would be

[(7, 'hello'), (1, 'fox')]

因此,l[0]将是(7, 'hello')l[1]将是(1, 'fox')(反之亦然,因为您不知道按键将以什么顺序出现).您实际上想要访问的是(7, 1)('hello', 'fox'),您需要使用诸如列表理解之类的东西:

so l[0] will be (7, 'hello') and l[1] will be (1, 'fox') (or vice-versa, since you don't know which order the keys will come out in). What you actually want to access is (7, 1) and ('hello', 'fox'), for which you either need to use something like a list comprehension:

[x[0] for x in l] # (7, 1)
[x[1] for x in l] # ('hello', 'fox')

或者您可以将其转换为NumPy数组并将其转置.

or you could convert it to a NumPy array and transpose that.

npl = numpy.array(l) # array([[7, 'hello'], [1, 'fox']])
nplT = npl.T         # array([[7, 1], ['hello', 'fox']])

现在您可以使用nplT[0],依此类推.

Now you can use nplT[0] and so on.

这篇关于以元组为键将Dictionary转换为Dataframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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