【发布时间】:2019-10-21 07:55:14
【问题描述】:
我想将 Pandas 系列转换为转置数据帧,其中键/值的数量是动态的。然后,转置后的 Dataframe 也必须具有动态列数。
我成功使用了 to_frame() 和 to_transpose() 方法,但我想优化我的代码。 更准确地说,我需要使用 reset_index() 方法,然后删除“索引”创建的无用列......我认为它可以更好地实现。
请找到我当前的代码:
current_case_details = row.to_frame().transpose().reset_index()
current_case_details.drop(columns=['index'], inplace=True)
print("CURRENT CASE DETAILS:\n{0}\n".format(current_case_details))
请找到预期结果的说明:picture of expected result
您有任何解决方案来使用“标准”Pandas 系列/数据框方法/选项优化我的代码吗?
感谢您的帮助:)
【问题讨论】:
标签: python python-3.x pandas dataframe series