【问题标题】:Converting dictionary of array to single pandas data frame将数组字典转换为单个熊猫数据框
【发布时间】:2021-06-18 20:11:38
【问题描述】:

我有这本词典(pred)。字典包含 numpy 数组。

pred.keys()       
dict_keys([0, 1])  #keys of the dictionary
pred.values()   #### values of the array nested in dictionary 
dict_values([array([[0.],
       [0.],
       [0.],
       ...,
       [0.],
       [0.],
       [0.]], dtype=float32), array([[0.],
       [0.],
       [0.],
       ...,
       [0.],
       [0.],
       [0.]], dtype=float32)])
I want to convert it into a data frame with each column showing the values of array for the dictionary key

【问题讨论】:

  • 你可以试试pd.concat({k: pd.DataFrame(v) for k, v in pred.items()}, axis=1) 吗?
  • @jerael.. 非常感谢您救命
  • 超级,添加到答案中。

标签: python arrays pandas dictionary multidimensional-array


【解决方案1】:

concat 与 DataFrame 构造函数一起使用:

pd.concat({k: pd.DataFrame(v) for k, v in pred.items()}, axis=1)

如果只需要DataFrame的第一列:

pd.concat({k: pd.DataFrame(v)[0] for k, v in pred.items()}, axis=1)

【讨论】:

    猜你喜欢
    • 2019-07-18
    • 2018-07-14
    • 2019-05-07
    • 2017-11-22
    • 2020-12-01
    • 2017-12-12
    • 2018-11-25
    • 1970-01-01
    相关资源
    最近更新 更多