【问题标题】:Extract a table out of data frame in pandas based on a condition根据条件从熊猫的数据框中提取表格
【发布时间】:2019-06-05 17:30:44
【问题描述】:

这里是 pandas 数据框的列名

result.columns.values

['A''B''C''D''E''F''G''H''I''J']

当我尝试使用过滤掉

filtered = result[(result['A'] <result['C']<result['D']) and (result['F'] <result['G']<result['I']) ] 

我得到这个 ValueError

ValueError:Series 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。

怎么了? 我该如何纠正这个问题

【问题讨论】:

    标签: python-3.x pandas


    【解决方案1】:

    我相信你需要这样的结构:

    filtered = result[((result['A'] < result['C']) & (result['C'] < result['D'])) &
                      ((result['F'] < result['G']) & (result['G'] < result['I']))]
    

    示例

    import numpy as np
    
    columns = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
    np.random.seed(0)
    result = pd.DataFrame(np.random.randint(0, 101, size=(100, 10)), columns=columns)
    result.head()
    
    filtered = result[((result['A'] < result['C']) & (result['C'] < result['D'])) &
                      ((result['F'] < result['G']) & (result['G'] < result['I']))]
    filtered
    

    [出]

         A   B   C   D   E  F   G   H   I   J
    7   35  11  46  82  91  0  14  99  53  12
    28  34  69  53  80  62  8  61   1  81  35
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-25
      • 2017-06-26
      • 1970-01-01
      • 2019-03-24
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      • 2021-11-02
      相关资源
      最近更新 更多