首页 > 解决方案 > 如何将一列dict转换为pandas中的单独列?

问题描述

这是一个示例 df,以 authid 作为索引。我想将功能分成几列。最初的特征列是用 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.xpandasdataframedictionarysplit

解决方案


这是你想要的?

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

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


推荐阅读