擅长:python、mysql、java
<p>尝试:</p>
<pre class="lang-py prettyprint-override"><code>df.columns = [re.sub(r"y(\d+)(.*)", r"\2-\1", c) for c in df.columns]
x = (
pd.wide_to_long(
df, stubnames=["", "gp", "rev"], sep="-", i="refnum", j="Base Year"
)
.rename(columns={"": "year"})
.reset_index()
.sort_values(by="refnum")
)
print(x)
</code></pre>
<p>印刷品:</p>
<pre class="lang-none prettyprint-override"><code> refnum Base Year year gp rev
0 10001 1 2021 200 300
3 10001 2 2022 600 100
6 10001 3 2023 300 300
1 10002 1 2020 200 300
4 10002 2 2021 500 200
7 10002 3 2022 300 300
2 10003 1 2021 200 300
5 10003 2 2022 500 500
8 10003 3 2023 300 300
</code></pre>