【问题标题】:Pandas dictionaries to dataframe熊猫字典到数据框
【发布时间】:2017-08-02 18:09:54
【问题描述】:

我有以下熊猫数据框:

                                                 Data
0   {'high': 630.99, 'low': 596.57, 'open': 624.79...
1   {'high': 612.55, 'low': 607.06, 'open': 608.52...
2   {'high': 612.40, 'low': 605.92, 'open': 608.23...
...

如何将其转换为如下数据框:

  high   low    open   ...
0 630.99 596.57 624.79 ...
1 612.55 607.06 608.52 ...
2 612.40 605.92 608.23 ...

【问题讨论】:

  • 您的问题是什么?你试过什么?

标签: python pandas dictionary dataframe python-3.5


【解决方案1】:

您可以先将dict 转换为numpy array 通过values,然后转换为list,最后使用DataFrame 构造函数:

df = pd.DataFrame(df.Data.values.tolist(), index=df.index)
print (df)
     high     low    open
0  630.99  596.57  624.79
1  612.55  607.06  608.52
2  612.40  605.92  608.23

【讨论】:

    猜你喜欢
    • 2016-10-17
    • 2017-02-27
    • 2020-07-23
    • 1970-01-01
    • 2021-07-17
    • 2020-07-22
    • 2021-10-26
    • 2023-03-23
    • 2018-06-24
    相关资源
    最近更新 更多