【问题标题】:apply function to two pandas dataframes in python (scipy.stats.spearmanr for each row from two dataframes)将函数应用于python中的两个pandas数据帧(来自两个数据帧的每一行的scipy.stats.spearmanr)
【发布时间】:2018-12-30 09:34:13
【问题描述】:

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

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

    |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|

销售数据框(见下文)记录每年(索引)中每种产品(列)的销售额

    |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 函数。

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

    |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|

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

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

【问题讨论】:

    标签: pandas dataframe apply


    【解决方案1】:

    猜猜你能做到

    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
    

    【讨论】:

    • 我如何在带有列的 df 中转换这个答案?
    • @Vivian 你是什么意思?
    • 我希望它返回一个带有“year”和“spearman corr”列的df
    • 最后打电话给.reset_index()
    • 我还有一个问题。在我的情况下,相关性返回一些值,如“NaN”,但在我比较所有列的两个 dfs 中,都有值(“float64”)。会是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-30
    • 2018-10-30
    • 2021-02-26
    • 2018-10-19
    • 2020-09-18
    • 2021-01-11
    相关资源
    最近更新 更多