【问题标题】:How can I get column name from correlation list?如何从相关列表中获取列名?
【发布时间】:2022-01-02 18:20:05
【问题描述】:

我想获取 corr 关系大于 0.2 且小于 0.8 的所有列名。有没有办法做到这一点?

【问题讨论】:

  • 嗯,你能得到值超过0.2的行吗?你能得到值小于 0.8 的行吗?为什么不把这两个条件放在一起呢?如果您对此不熟悉,那么您可能应该只阅读 Pandas 教程;这是关于你可以用 Dataframe 做的最基本的事情,也是首先使用 Pandas 的原因。您实际上是从哪里得知有 Pandas 这样的东西的?您应该回到那里,更仔细地阅读有关如何使用它的建议。

标签: python pandas dataframe correlation


【解决方案1】:

您可以使用条件索引系列corList,并使用.index检索名称:

corList[(corList > 0.2) & (corList 0.8)].index

或者更易读的版本:

corList[corList.gt(0.2) & corList.lt(0.8)].index

【讨论】:

    【解决方案2】:

    使用 pandas 文档中的示例,我们可以得到两个条件的 corr 和 filter,获取匹配项的索引并输出到列表。

    import pandas as pd
    def histogram_intersection(a, b):
        v = np.minimum(a, b).sum().round(decimals=1)
        return v
    df = pd.DataFrame([(.2, .3), (.0, .6), (.6, .0), (.2, .1)],
                      columns=['dogs', 'cats'])
    
    c = abs(df.corr(method=histogram_intersection)['cats'])
    
    print(c)
    print(c[(c>.2) & (c<.8)].index.tolist())
    

    输出

    dogs    0.3
    cats    1.0
    Name: cats, dtype: float64
    
    ['dogs']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-10
      • 2012-05-02
      • 2010-10-15
      • 2011-12-03
      • 2017-08-14
      • 2011-11-08
      相关资源
      最近更新 更多