簡體   English   中英

如何將一列dict轉換為pandas中的單獨列?

[英]How to convert a column of dict into separate columns in 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

這是你想要的?

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

順便說一句,我將第三列名稱假定為您在輸入示例中未提及的值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM