简体   繁体   中英

Better way to apply function to every combination of two columns in Pandas.DataFrame

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM