Pandas将列标题添加到行值并转换DataFrame

发布于 2025-02-12 07:37:02 字数 1089 浏览 2 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仄言 2025-02-19 07:37:02

尝试使用融化

out = df.melt(['ID','Date'],var_name = 'ID_TEST',value_name='Result')
out['ID_TEST'] = out['ID'] + '-' + out['ID_TEST']

Try with melt

out = df.melt(['ID','Date'],var_name = 'ID_TEST',value_name='Result')
out['ID_TEST'] = out['ID'] + '-' + out['ID_TEST']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文