转换电子表格(Excel 或 Google 表格)中的数据,以便每 N 行自动填充

Transforming data in spreadsheet (Excel or Google Sheets) such that every N rows gets autofilled

假设我有一个电子表格如下:

name x1 x2 x3
a 4 8 9
b 5 2 6
c 7 3 1

我想要它的格式

name var value
a x1 4
a x2 8
a x3 9
b x1 5
b x2 2
b x3 6
c x1 7
c x2 3
c x3 1

在 Google 表格中完成此操作的最佳方法是什么?或者我最好只转换 Python/R 中的数据?

编辑:感谢大家在电子表格中提供的出色解决方案。我发现使用 Python 进行转换更简单,但我很欣赏新发现的电子表格知识!

={"name","var","value";
index(split(flatten(A2:A4&"❄️"&B1:D1&"❄️"&B2:D4),"❄️"))}

参考:Unpivot In Google Sheets

使用:

=INDEX(QUERY(SPLIT(FLATTEN(A2:A&"×"&B1:D1&"×"&B2:D); "×"); "where Col3 is not null"))