I am trying to prepare some data for a heatmap or 3D plot. The general idea is that I have some function z=f(x,y) where z is the value of a specific cell with x as its column value and y as its index value.
My current approach is to loop over the dataframe which already shows the desired result:
import numpy as np
import pandas as pd
def my_fun(a, b):
return(a**2 + b**3)
index = [i for i in np.arange(25.0, 100.0, 25.0)]
columns = [i for i in np.arange(150.0, 600.0, 150.0)]
df = pd.DataFrame(np.zeros((3, 3)), index=index, columns=columns)
for idx in index:
for col in columns:
df.loc[idx, col] = my_fun(idx, col)
print(df)
and yields:
150.0 300.0 450.0
25.0 3375625.0 27000625.0 91125625.0
50.0 3377500.0 27002500.0 91127500.0
75.0 3380625.0 27005625.0 91130625.0
But looping over the dataframe is probably not the right (vectorized) way to deal with this problem and I was looking for some pretty combination of apply/applymap/map.
Is there any way to get the same result in a smarter/vectorized way?
Thanks in advance!