Pandas将列标题添加到行值并转换DataFrame
我有一个Excel Workbook,其中包含多个纸张,如以下内容:
[ID] [Date] [TEST_A] [TEST_B] [TEST_C]
ID_1234 13/06/2017 11:00 1194.256258 1287.016744 1343.434
ID_1234 13/06/2017 12:00 1194.266828 1287.16688 1463.2352
...
每个纸对于[ID]列都有不同的值。有些床单上升到test_e,有些则只有test_a,它们也不总是以相同的顺序。
我试图通过创建一个新的ID_Test列来转换数据框,该列与ID和每个测试列连接在一起,并将其余的列转换为与ID_TEST对齐。然后,每个纸将具有以下四个列,新结果列将具有每个单独的测试列的值。
[ID] [ID_TEST] [Date] [Result]
ID_1234 ID_1234-TEST_A 13/06/2017 11:00 1194.256258
ID_1234 ID_1234-TEST_A 13/06/2017 12:00 1194.266828
ID_1234 ID_1234-TEST_B 13/06/2017 11:00 1287.016744
ID_1234 ID_1234-TEST_B 13/06/2017 12:00 1287.1688
ID_1234 ID_1234-TEST_C 13/06/2017 11:00 1343.434
ID_1234 ID_1234-TEST_C 13/06/2017 12:00 1463.2352
我尝试使用此产品开始创建[ID_TEST]列,
df.apply(lambda col: col.name +" "+ col.astype(str) )
但这适用于每个列,而不是我想要的专栏。我认为这也需要一个枢轴表或一些东西来重组DF,但我不确定如何实施它。谢谢。
I have an excel workbook with multiple sheets laid out like the following:
[ID] [Date] [TEST_A] [TEST_B] [TEST_C]
ID_1234 13/06/2017 11:00 1194.256258 1287.016744 1343.434
ID_1234 13/06/2017 12:00 1194.266828 1287.16688 1463.2352
...
Each sheet has a different value for the [ID] column. Some sheets go up to TEST_E and some only have TEST_A, they are not always in the same order either.
I am trying to transform the dataframe by creating a new ID_TEST column that joins the ID and each TEST columns, and transforming the rest of the columns to align with ID_TEST. Every sheet will then have four columns as follows, where the new Result column will have the values from each individual TEST column.
[ID] [ID_TEST] [Date] [Result]
ID_1234 ID_1234-TEST_A 13/06/2017 11:00 1194.256258
ID_1234 ID_1234-TEST_A 13/06/2017 12:00 1194.266828
ID_1234 ID_1234-TEST_B 13/06/2017 11:00 1287.016744
ID_1234 ID_1234-TEST_B 13/06/2017 12:00 1287.1688
ID_1234 ID_1234-TEST_C 13/06/2017 11:00 1343.434
ID_1234 ID_1234-TEST_C 13/06/2017 12:00 1463.2352
I have tried starting with creating the [ID_TEST] column using this
df.apply(lambda col: col.name +" "+ col.astype(str) )
But this applies to every column and not the ones I want specifically. I think this will also need a pivot table or something to restructure the df but I am not sure how to implement it. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用
融化
Try with
melt