Pandas DataFrame列与自定义函数的成对关联 [英] Pairwise correlation of Pandas DataFrame columns with custom function

查看:125
本文介绍了Pandas DataFrame列与自定义函数的成对关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在DataFrame上

Pandas 成对关联十分方便很多情况下.但是,在我的特定情况下,我想使用Pandas未提供的方法(除(Pearson,kendall或spearman之外的)东西来关联两列.是否可以明确定义在这种情况下要使用的关联函数?

我想要的语法如下:

def my_method(x,y): return something
frame.corr(method=my_method)

解决方案

您需要在cython中针对任何类型的perf(具有可cythonizable函数)进行此操作

l = len(df.columns)
results = np.zeros((l,l))
for i, ac in enumerate(df):
    for j, bc in enumerate(df):
           results[j,i] = func(ac,bc)
results = DataFrame(results,index=df.columns,columns=df.columns)

Pandas pairwise correlation on a DataFrame comes handy in many cases. However, in my specific case I would like to use a method not provided by Pandas (something other than (pearson, kendall or spearman) to correlate two columns. Is it possible to explicitly define the correlation function to use in this case?

The syntax I would like looks like this:

def my_method(x,y): return something
frame.corr(method=my_method)

解决方案

You would need to do this in cython for any kind of perf (with a cythonizable function)

l = len(df.columns)
results = np.zeros((l,l))
for i, ac in enumerate(df):
    for j, bc in enumerate(df):
           results[j,i] = func(ac,bc)
results = DataFrame(results,index=df.columns,columns=df.columns)

这篇关于Pandas DataFrame列与自定义函数的成对关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆