将函数应用于python中的两个 pandas 数据帧(两个数据帧中的每一行的scipy.stats.spearmanr) [英] apply function to two pandas dataframes in python (scipy.stats.spearmanr for each row from two dataframes)

查看:69
本文介绍了将函数应用于python中的两个 pandas 数据帧(两个数据帧中的每一行的scipy.stats.spearmanr)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个熊猫数据框:价格和销售数据框.

I have two panda dataframe: price and sales dataframe.

价格数据框记录了每年(索引)中每种产品(列)的价格

price dataframe records price for each product (columns) in each year (index)

    |a  |b  |c  |d  |e  |
2018|3.2|4.5|5.6|7.8|8.1|
2017|6.2|1.5|2.6|7.8|2.1|
2016|2.2|9.5|0.6|6.8|4.1|
2015|2.2|6.5|7.6|7.8|2.1|

sales数据框(如下所示)记录了每年(索引)中每种产品(列)的销售额

sales dataframe (see below) records sales for each product (columns) in each year (index)

    |a  |b  |c  |d  |e  |
2018|101|405|526|108|801|
2017|601|105|726|308|201|
2016|202|965|856|408|411|
2015|322|615|167|458|211|

我想计算每年价格和销售额之间的Spearman相关性.我知道scipy.stats.spearmanr函数可以完成类似的工作,但是我需要对两个数据帧中的每一行都应用scipy.stats.spearmanr功能.

I would like to calculate spearman correlation between price and sales for each year. I know scipy.stats.spearmanr function does the similar job, but I need to apply scipy.stats.spearmanr fucction for each row in the two dataframes.

例如,对于2018年,我需要计算之间的Spearman相关性

For example, for 2018, i need to calculate the spearman correlation between

    |a  |b  |c  |d  |e  |
2018|3.2|4.5|5.6|7.8|8.1|

    |a  |b  |c  |d  |e  |
2018|101|405|526|108|801|

我可以知道什么是最好的吗? 结果我想要类似下面的输出:

May I know what is the best to do that? The results i want a output like below:

2018|spearman cor btw price and sales in 2018
2017|spearman cor btw price and sales in 2017
2016|spearman cor btw price and sales in 2016

推荐答案

猜猜你可以做

import scipy.stats as st

>>> pd.Series(map(lambda k: st.spearmanr(k[0], k[1])[0],
                  zip(df.values, df2.values)),    
              index=df.index)
2018    0.7
2017    0.6
2016    0.3
2015    0.2
dtype: float64

这篇关于将函数应用于python中的两个 pandas 数据帧(两个数据帧中的每一行的scipy.stats.spearmanr)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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