python-3.x - 如何将一列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
解决方案
这是你想要的?
选项 1:
df = df.reset_index().pivot(index='authid', columns='features', values='value')
选项 2:
df.unstack()
顺便说一句,我将第三列名称假定为您在输入示例中未提及的值
推荐阅读
- java - 在静态上下文中使用 Ehcache
- java - 我可以从 java 中使用 Koin 的“by sharedViewModel”吗?
- c# - AspNetCore 中的属性绑定
- css - 何时在 css 元素和类选择器之间放置一个空格?
- javascript - 如何使用foreach访问javascript中的嵌套对象
- c - 在c中使函数指针成为结构的成员
- javascript - My Discord bot is not working on a second server
- javascript - 根据上下文对对象进行不同的字符串化
- sass - sass 生成的 source map 中的文件名转换为小写
- python - 使用 Pandas 消除原始 CSV 文件中的空格后制作新的 CSV 文件