Home > Enterprise >  Pandas apply a function using index name and column name
Pandas apply a function using index name and column name

Time:10-27

Consider the dataframe:

data = pd.DataFrame(0, index=[1,2,3,4], columns=[1,2,3,4])
data    
    1   2   3   4
1   0   0   0   0
2   0   0   0   0
3   0   0   0   0
4   0   0   0   0

I want to fill the values using a function that takes two arguments. These two arguments will be the index name and the column name of the dataframe. How can I achieve this (using vectorization)?

def multiply(a, b):
    return a * b

I have been trying various combinations of data.apply(lambda x: x.apply(multiply, args=(x.name))) but could not make it work.

Any help is appreciated greatly.

CodePudding user response:

If use apply there are loops under the hood, so not vecorized solution, If need multiple index by columns names use numpy soluton like:

a = data.index.to_numpy() * data.columns.to_numpy()[:, None]
print (a)
[[ 1  2  3  4]
 [ 2  4  6  8]
 [ 3  6  9 12]
 [ 4  8 12 16]]

data[:] = data.index.to_numpy() * data.columns.to_numpy()[:, None]
print (data)
   1  2   3   4
1  1  2   3   4
2  2  4   6   8
3  3  6   9  12
4  4  8  12  16

CodePudding user response:

You can make your code work, using:

def multiply(a, b):
    return a * b

df = df.apply(lambda x: multiply(x.index, x.name))
  • Related