Pandas 将函数应用于python中的两个数据帧(两个数据帧中的每一行的scipy.stats.spearmanr)
我有两个熊猫数据框:价格和销售数据框 价格数据框记录每年每个产品(列)的价格(索引) sales dataframe(见下文)记录每年每个产品(列)的销售额(索引) 我想计算一下斯皮尔曼每年的价格和销售额之间的相关性。我知道scipy.stats.spearmanr函数做类似的工作,但我需要为两个数据帧中的每一行应用scipy.stats.spearmanr函数 例如,对于2018年,我需要计算Pandas 将函数应用于python中的两个数据帧(两个数据帧中的每一行的scipy.stats.spearmanr),pandas,dataframe,apply,Pandas,Dataframe,Apply,我有两个熊猫数据框:价格和销售数据框 价格数据框记录每年每个产品(列)的价格(索引) sales dataframe(见下文)记录每年每个产品(列)的销售额(索引) 我想计算一下斯皮尔曼每年的价格和销售额之间的相关性。我知道scipy.stats.spearmanr函数做类似的工作,但我需要为两个数据帧中的每一行应用scipy.stats.spearmanr函数 例如,对于2018年,我需要计算 |a |b |c |d |e | 2018|3.2|4.5|5.6|7.8|8.1
|a |b |c |d |e |
2018|3.2|4.5|5.6|7.8|8.1|
及
我可以知道做那件事最好的方法是什么吗?
结果我希望输出如下所示:
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
|a |b |c |d |e |
2018|101|405|526|108|801|
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