python - 如何合并不同的dfs并取消堆叠值?
问题描述
我有一个里面dict
有17个df
。
样品dfs
:
df1
key percent
0 step19_without_lof 14.534883720930232
df2
key percent
0 step19_without_lof 14.970930232558139
df3
key percent
0 step1_without_lof 1.5988372093023255
1 step2_without_lof 30.377906976744185
2 step5_without_lof 3.197674418604651
3 step7_without_lof 9.738372093023257
4 step12_without_lof 5.377906976744186
5 step15_without_lof 4.215116279069767
6 step16_without_lof 6.8313953488372094
7 step19_without_lof 13.80813953488372
8 step24_without_lof 9.883720930232558
9 step25_without_lof 11.337209302325581
10 step26_without_lof 9.738372093023257
11 step27_without_lof 9.738372093023257
等等。
我想以dfs
这样的方式合并这些key
列成为每列的名称并填写相应的值。在df1
anddf2
由于只有一个key
,其余的keys
必须填写nans
。
解决方案
你可以这样做:
# append the key to the index (first level is the old index)
# then unstack the key, so the key is converted to columns
df.set_index('key', append=True).unstack('key')
推荐阅读
- c# - 使用HttpClient分块上传大文件到Controller,IFormFile总是为空
- dart - 动态更改 AlertDialog 标题 (showDialog())
- python - 如何使用熊猫转换我的 Excel 文件中的所有列
- r - 为通过 setRefClass 创建的对象扩展 as.data.frame 和 as.matrix 方法
- arrays - Google Scripts:使用单元格值填充多维数组
- powershell - 需要在powershell脚本中修剪一个url
- serialization - 有没有办法告诉 Serde 使用 struct 字段作为地图的键?
- java - 使用 SoapUi 生成的 POJO
- uwp - 如何在 .IDL 文件中声明枚举?
- xcode - 运行 git status 时,Mojave 更新后发生以下错误:“xcrun: error: invalid active developer path”