将数据帧类型对象转换为字典

时间:2017-01-27 14:06:13

标签: python pandas dictionary dataframe

我有数据框best_scores contanining

     subsample colsample_bytree learning_rate max_depth min_child_weight  \
3321       0.8              0.8           0.3         2                3   

            objective  scale_pos_weight  silent  
3321  binary:logistic          1.846154       1  

我想将其转换为字典params,如:

params
{'colsample_bytree': 0.8,
  'learning_rate': 0.3,
  'max_depth': 2,
  'min_child_weight': 3,
  'objective': 'binary:logistic',
  'scale_pos_weight': 1.8461538461538463,
  'silent': 1,
  'subsample': 0.8}

但如果我跑

best_scores.to_dict(orient='records')

我明白了:

[{'colsample_bytree': 0.8,
  'learning_rate': 0.3,
  'max_depth': 2,
  'min_child_weight': 3,
  'objective': 'binary:logistic',
  'scale_pos_weight': 1.8461538461538463,
  'silent': 1L,
  'subsample': 0.8}]

你能帮忙吗?

1 个答案:

答案 0 :(得分:1)

您正在获取字典列表,因为您要将DataFrame转换为dict,这可能包含多行。每行都是列表中的一个条目。

除了提到的简单选择第一个条目的解决方案之外,实现目标的理想方法是使用Series而不是DataFrame。这样,只返回一个dict

In [2]: s = pd.Series([1, 2 ,3], index=['a', 'b', 'c'])

In [3]: s.to_dict()
Out[3]: {'a': 1, 'b': 2, 'c': 3}

In [4]: d = pd.DataFrame(s).T

In [5]: d
Out[5]: 
   a  b  c
0  1  2  3

In [6]: d.iloc[0]
Out[6]: 
a    1
b    2
c    3
Name: 0, dtype: int64

In [7]: d.iloc[0].to_dict()
Out[7]: {'a': 1, 'b': 2, 'c': 3}
相关问题