我找到了一种在字典中连接我的 pandas 数据框的方法。 阙;我如何使用字典键作为最终数据帧中的第一列? 我试过了,但没成功
pd.concat(test_list.values(), ignore_index=False, keys= test_list.keys())
数据
test_list = {'ENSMUSG00000025333.10': treatment_code subjectCode fraction_modified_entropy_in_log2 log_RPKM
59478 0 2 1.822939 3.109444
107087 1 4 0.811278 3.476707, 'ENSMUSG00000025903.14': treatment_code subjectCode fraction_modified_entropy_in_log2 log_RPKM
178498 0 1 1.000000 3.175110
107116 1 3 3.551271 3.002025, 'ENSMUSG00000063663.11': treatment_code subjectCode fraction_modified_entropy_in_log2 log_RPKM
59417 0 2 2.755851 1.07040
118918 1 3 3.088372 1.19147}
您可以在 reset_index
之后
concat
,可选择使用 name=['desired name']
设置名称:
out = (pd.concat(test_list.values(), ignore_index=False,
keys= test_list.keys(),
names=['col1'])
.reset_index(0)
)
注意,由于你已经有了字典,所以可以直接传递它,而不是使用
.keys()
/.values()
:
out = (pd.concat(test_list, ignore_index=False,
names=['col1'])
.reset_index(0)
)
输出:
col1 treatment_code subjectCode fraction_modified_entropy_in_log2 log_RPKM
59478 ENSMUSG00000025333.10 0 2 1.822939 3.109444
107087 ENSMUSG00000025333.10 1 4 0.811278 3.476707
178498 ENSMUSG00000025903.14 0 1 1.000000 3.175110
107116 ENSMUSG00000025903.14 1 3 3.551271 3.002025
59417 ENSMUSG00000063663.11 0 2 2.755851 1.070400
118918 ENSMUSG00000063663.11 1 3 3.088372 1.191470