Pandas Crosstab With Own Function
I have a function which takes two inputs and returns a float e.g. my_func('A', 'B') = 0.5. I have a list of possible inputs: x = ['A', 'B', 'C', 'D', 'E', 'F']. I want to produce
Solution 1:
Option 1 brute force
xt = pd.DataFrame(index=x, columns=x)
foriin x:
forjin x:
xt.set_value(i, j, my_func(i, j))
Demo
defmy_func(i,j):returnord(i)*ord(j)x= ['A', 'B', 'C', 'D', 'E', 'F']
xt=pd.DataFrame(index=x,columns=x)for i in x:for j in x:xt.set_value(i,j,my_func(i,j))xtABCDEFA4225 4290 4355 4420 4485 4550B4290 4356 4422 4488 4554 4620C4355 4422 4489 4556 4623 4690D4420 4488 4556 4624 4692 4760E4485 4554 4623 4692 4761 4830F4550 4620 4690 4760 4830 4900
Option 2
idx=pd.MultiIndex.from_product([x,x])pd.Series(idx.map(lambdax:my_func(*x)),idx).unstack()ABCDEFA4225 4290 4355 4420 4485 4550B4290 4356 4422 4488 4554 4620C4355 4422 4489 4556 4623 4690D4420 4488 4556 4624 4692 4760E4485 4554 4623 4692 4761 4830F4550 4620 4690 4760 4830 4900
Solution 2:
considering the brute force method, it is recommended to use the .loc method, i.e.
for i in x:
for j in x:
xt.loc[i,j] = my_func(i,j)
instead of:
for i in x:
for j in x:
xt.set_value(i, j, my_func(i, j))
because set_value is deprecated and will be removed in a future release.
Also, .at[]
or .iat[]
acessors may be used.
Post a Comment for "Pandas Crosstab With Own Function"