如何将列中的值与数据框中存在的另一列的名称相匹配,然后计算相应的百分比变化?

How to match the value in the column with the name of the other column present in the dataframe and then calculate the corresponding percent change?

我正在处理如下所述的类似数据集:

ID col1 col2 col3 col4 col5
1 col3 10 9 5 4
2 col5 6 7 4 8
3 col3 12 4 2 11
4 col4 9 5 8 10

在 col1 中,数据具有同一数据框的某些列名的值。 我需要将 col1 中的值与数据框中列的实际名称相匹配,然后使用 col2.

计算值的百分比变化

预期输出 table 可以表示为:

ID col1 col2 col3 col4 col5 percent_change
1 col3 10 9 5 4 (10-9)/9*100 is 11.11%
2 col5 6 7 4 8 (6-8)/8*100 is -25%
3 col3 12 4 2 11 (12-4)/4*100 is 200%
4 col4 9 5 8 10 (9-8)/8*100 is 12.5%

因此对于 ID:1,col1 的值为 col3。所以代码应该找到 col3 并计算它下面的值与 col2.

的百分比

同样,对于ID:2,col1的值为col5,所以代码应该找到col5,并找到col2中对应值的百分比变化。即这里col5为8,col2为6。然后计算百分比变化。

我尝试了一些方法但得到了错误的输出。

请帮助我 python pandas 代码。

通过lookup获取值,然后用算术运算计算新列:

idx, cols = pd.factorize(df['col1'])

s = df.reindex(cols, axis=1).to_numpy()[np.arange(len(df)), idx]

df['percent_change'] = df['col2'].sub(s).div(s).mul(100)

print (df)
   ID  col1  col2  col3  col4  col5  percent_change
0   1  col3    10     9     5     4       11.111111
1   2  col5     6     7     4     8      -25.000000
2   3  col3    12     4     2    11      200.000000
3   4  col4     9     5     8    10       12.500000