Reputation: 652
I want to implement a something just like DataFrame.corr()
which can apply a function to pairwise columns.
Eg.
I have a function:
def func(x, y):
pass
I want to apply func
to every combination of two columns in a_pd
(type of Pandas.DataFrame
). I have figured out a way by create a new function wap_func
to wrap func
:
def wap_func(x):
for i in range(len(x)):
for j in range(i+1, len(x)):
func(x[i], x[j])
res = a_pd.apply(wap_func, axis=1)
Although the question seems to be solved, but it isn't convenient. If it could be done like a_pd.corr()
, it could be better.
Upvotes: 4
Views: 1617
Reputation: 838
Have you considered using the itertools.combinations
module?
import pandas as pd
from itertools import combinations
df = pd.DataFrame([[1,2,3], [2,3,4], [3,5,7]], columns = ['A', 'B', 'C'])
print(df)
A B C
0 1 2 3
1 2 3 4
2 3 5 7
Define your function slightly differently so that you can use apply more seamlessly
def func(xy):
x, y = xy
return x+y
Use the itertools.combinations
module to get all combinations of the columns that you wish, go through each of the combinations in turn, and apply the function earlier defined
for combi in combinations(df.columns, 2):
df['_'.join([i for i in combi])] = df[[i for i in combi]].apply(func, axis=1, result_type='expand').transpose().values
print(df)
A B C A_B A_C B_C
0 1 2 3 3 4 5
1 2 3 4 5 6 7
2 3 5 7 8 10 12
Upvotes: 1