从作为字典中值的熊猫系列中提取索引

时间:2021-07-27 19:51:16

标签: python pandas

我有一个字典,其中键是字符串类型,值是具有“索引值”结构的熊猫系列。我想提取 Pandas 系列索引,并将它们放在另一个字典中,每个索引都有值。有什么方法可以将一本字典转换成另一本字典?谢谢。

这是我正在使用的字典示例:

value = pd.Series([5,5,5])
key = ['one', 'two', 'three']
dic = {}

for i in key:
        dic[i] = value      
print(dic)

2 个答案:

答案 0 :(得分:0)

这应该有效。

df = pd.DataFrame(your_dic).T #transpose
new_dic = df.to_dict()

答案 1 :(得分:0)

如果我不够清楚,我很抱歉。我会更新帖子。

我想出了另一种方法:

rec = pd.Series([])

for item in key:
    rec=rec.append(dic[item])