问题描述
我有两个熊猫数据框:价格和销售数据框.
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 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 用于来自两个数据帧的每一行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!