【问题标题】:How to convert a column of dict into separate columns in pandas?如何将一列dict转换为pandas中的单独列?
【发布时间】:2021-06-09 23:59:04
【问题描述】:

这是一个示例 df,以 authid 作为索引。我想将功能分成几列。原来 features 列是用 dict 填充的。

我试过 df['features'].apply(pd.Series) 但没有用。

authid              features                        
108560005        n_clusters           2.0000
                 inertia             23.4968
                 silhouette           0.9061
                 trend_datapoints   355.0000
                 trend_slope         -0.0010
                 trend_pvalue         0.0615
                 resid_std         1075.9582
123750005        n_clusters           2.0000
                 inertia              0.0682
                 silhouette           0.9961
                 trend_datapoints   355.0000
                 trend_slope          0.0046
                 trend_pvalue         0.0000
                 resid_std           75.8126

【问题讨论】:

    标签: python-3.x pandas dataframe dictionary split


    【解决方案1】:

    这是你要找的吗?

    选项 1: df = df.reset_index().pivot(index='authid', columns='features', values='value') 选项 2: df.unstack()

    顺便说一句,我假设第三列名称为您在输入示例中未提及的值

    【讨论】:

    • 没有第三列。所有这些都在功能下
    • 我不确定这怎么可能,我将 2.0 视为 authid - 108560005 的 n_clusters 功能的值,这是不正确的理解吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-07
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    • 2021-10-10
    • 1970-01-01
    相关资源
    最近更新 更多