我有一本这样的字典:
pred_dict = {('african zebra', 'arabian horse'): [('Blue Whale', 0.49859235), ('Ferrari', 0.5013809), ('african zebra', 0.49264234), ('ara
...: bian horse', 0.5186422), ('bobcat', 0.5096679)], ('cheetah', 'mountain lion'): [('Blue Whale', 0.48881102), ('Ferrari', 0.502793), ('afric
...: an zebra', 0.48751196), ('arabian horse', 0.49272105), ('bobcat', 0.5228181)]}
想转换成这样的数据框:
Text | Blue Whale | Ferrari | african zebra| arabian horse | bobcat |
('african zebra', 'arabian horse') 0.49859235 0.5013809 0.49264234 0.5186422 0.5096679
('cheetah', 'mountain lion') 0.48881102 0.502793 0.48751196 0.49272105 0.5228181
给定字典中的每个值都具有完全相同的元组数,并且元组列表中的第一个值相同。要做的是将字典的键放在“文本”列中,然后将元组中的第一个值设为其他列名。值将是分数-浮点数。
任何建议都会有所帮助。这是我现在正在尝试的一些东西:
In [12]: text = list(pred_dict.keys())
In [13]: values = list(pred_dict.values())
In [14]: pred_df = pd.DataFrame({'text': text, 'label_scores': values})
In [15]: pred_df
Out[15]:
text label_scores
0 (african zebra, arabian horse) [(Blue Whale, 0.49859235), (Ferrari, 0.5013809...
1 (cheetah, mountain lion) [(Blue Whale, 0.48881102), (Ferrari, 0.502793)...
In [19]: df_scores = pred_df['label_scores']
In [21]: df_scores
Out[21]:
0 [(Blue Whale, 0.49859235), (Ferrari, 0.5013809...
1 [(Blue Whale, 0.48881102), (Ferrari, 0.502793)...
Name: label_scores, dtype: object
In [22]: labels = [t[1] for t in df_scores[0]]
In [23]: labels
Out[23]: [0.49859235, 0.5013809, 0.49264234, 0.5186422, 0.5096679]
In [24]: labels = [t[0] for t in df_scores[0]]
In [25]: labels
Out[25]: ['Blue Whale', 'Ferrari', 'african zebra', 'arabian horse', 'bobcat']
In [26]: scores = [t[1] for t in df_scores[0]]
In [27]: scores
Out[27]: [0.49859235, 0.5013809, 0.49264234, 0.5186422, 0.5096679]
In [28]: scores = [t[1] for t in df_scores[1]]
In [29]: scores
Out[29]: [0.48881102, 0.502793, 0.48751196, 0.49272105, 0.5228181]
答案 0 :(得分:1)
x <- c(`?` = 17082, AA = 247, BB = 1813, CC = 96, DD = 72, EE = 12529, FF = 49732, GG = 382188, HH = 9)
round(prop.table(x[-1])*sum(x))
AA BB CC DD EE FF GG HH
256 1882 100 75 13008 51634 396803 9
这应该做到:
pred_dict = {('african zebra', 'arabian horse'): [('Blue Whale', 0.49859235), ('Ferrari', 0.5013809), ('african zebra', 0.49264234), ('arabian horse', 0.5186422), ('bobcat', 0.5096679)], ('cheetah', 'mountain lion'): [('Blue Whale', 0.48881102), ('Ferrari', 0.502793), ('african zebra', 0.48751196), ('arabian horse', 0.49272105), ('bobcat', 0.5228181)]}
答案 1 :(得分:0)
虽然不漂亮,但是可以工作:
pd.concat([pd.DataFrame(r,columns=['Text','value'],index=[t]*len(r)) for (t, r) in pred_dict.items()]).set_index('Text',append=True).unstack('Text')['value']
其输出是:
pred_dict = {
('african zebra', 'arabian horse'): [('Blue Whale', 0.49859235),
('Ferrari', 0.5013809),
('african zebra', 0.49264234),
('arabian horse', 0.5186422),
('bobcat', 0.5096679)],
('cheetah', 'mountain lion'): [('Blue Whale', 0.48881102),
('Ferrari', 0.502793),
('african zebra', 0.48751196),
('arabian horse', 0.49272105),
('bobcat', 0.5228181)]
}
df = pd.DataFrame(pred_dict).T
df.columns = [tuple[0] for tuple in list(df.iloc[0])]
df = df.apply(lambda x: [tuple[1] for tuple in x])
df.reset_index(inplace=True)
df.insert(0, "Text", list(zip(df.level_0, df.level_1)))
df.drop(["level_0", "level_1"], axis=1, inplace=True)
答案 2 :(得分:0)
好。经过一番试验,才得以做到。这是我的操作方式:
text = list(pred_dict.keys())
values = list(pred_dict.values())
df_1 = pd.DataFrame({'text': text})
score_dict = {}
for label in mlb_classes:
score_list = []
for t_list in values:
for t in t_list:
if t[0] == label:
score_list.append(t[1])
score_dict[label] = score_list
df_2 = pd.DataFrame(score_dict)
score_df = pd.concat([df_1, df_2], axis=1)
print(score_df)
输出:
text Blue Whale Ferrari african zebra arabian horse bobcat
0 (african zebra, arabian horse) 0.519343 0.511951 0.512639 0.527919 0.491461 0.516240
1 (cheetah, mountain lion) 0.495197 0.527627 0.497516 0.512571 0.488823 0.510277