to_dict()在值周围创建括号

问题描述 投票:1回答:2

我试图从我的pandas DataFrame创建默认字典,但to_dict()方法围绕我想写的列的值创建不需要的方括号。示例代码如下:

# Create DF
my_df = pd.DataFrame({'numbers': (1, 2, 3, 4, 5), 'letters': ('a', 'b', 'c', 'd', 'e')})

# Create dictionary from the DF
my_dict = my_df.set_index('numbers').T.to_dict('list')

# Create collections dictionary
my_collections_dict = collections.defaultdict(int, my_dict)

结果是:

defaultdict(int, {1: ['a'], 2: ['b'], 3: ['c'], 4: ['d'], 5: ['e']})

我想要的是:

defaultdict(int, {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'})

如何获得'纯'列值?

python pandas dictionary
2个回答
3
投票

您无需转置框架,而是可以选择列并执行以下操作:

my_dict = my_df.set_index('numbers')['letters'].to_dict()

如果您想在字典中使用多列,则需要额外的一行,但您可以使用:

my_dict = my_df.set_index('numbers').to_dict(orient='index')
my_dict = {k: list(v.values()) for k, v in my_dict.items()}

1
投票

这是因为你指定to_dict('list') - >这样条目将作为列表返回(这就是为什么它们显示在[]中。

尝试使用records代替:

# Create DF
my_df = pd.DataFrame({'numbers': (1, 2, 3, 4, 5), 'letters': ('a', 'b', 'c', 'd', 'e')})

# Create dictionary from the DF
my_dict = my_df.set_index('numbers').T.to_dict('records')

# Create collections dictionary
my_collections_dict = collections.defaultdict(int, my_dict)

第二行的输出:

[{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}]

© www.soinside.com 2019 - 2024. All rights reserved.