我只能考虑使用for循环和置换,下面是我的尝试。
import pandas as pd
import numpy as np
from itertools import permutations
df = pd.DataFrame(np.random.randint(0,5,size=(5, 3)), columns=list('ABC'))
func = lambda d,x,y: d[x]/d[y]
perms = [x for x in permutations('ABC', 2)]
for p in perms:
x,y = p
s = r'/'.join([x,y])
df[s]=func(df,x,y)
print(df)
A B C A/B A/C B/A B/C C/A C/B
0 0 4 1 0.000000 0.0 inf 4.00 inf 0.250000
1 4 3 4 1.333333 1.0 0.750000 0.75 1.000000 1.333333
2 3 0 2 inf 1.5 0.000000 0.00 0.666667 inf
3 4 2 1 2.000000 4.0 0.500000 2.00 0.250000 0.500000
4 3 0 1 inf 3.0 0.000000 0.00 0.333333 inf