【问题标题】:Checking multiple conditions against list within a list根据列表中的列表检查多个条件
【发布时间】:2021-07-11 01:17:34
【问题描述】:

我正在尝试根据两个条件从下面的示例数据集中过滤出实体,即它们的值是否在 0-180 或 180-360 之间。并计划创建两个单独的此类实体列表。 我的 df 是;

Entityname Value
A          200
A          240
A          330
B          15
B          120
C          190
C          220

预期输出:

Entities_1=['A','C']
Entities_2=['B']

以下是我一直在尝试的..

Entities_1=[]
Entities_2=[]

for name in df.Entityname:
    if df.Value > 0 & df.Value < 180:
        Entities_1.append(name)
    else:
        if df.Value > 180 & df.Value < 360:
            Entities_2.append(name)

在上面遇到一些错误,不确定这是否是正确的前进方向.. 任何帮助将不胜感激!..

【问题讨论】:

    标签: pandas list conditional-statements


    【解决方案1】:

    您可以使用pandas.Series.between() 和布尔索引。

    Entities_1 = df.loc[df['Value'].between(0, 180), 'Entityname'].unique().tolist()
    Entities_2 = df.loc[df['Value'].between(180, 360), 'Entityname'].unique().tolist()
    
    # print(Entities_1)
    ['B']
    
    # print(Entities_2)
    ['A', 'C']
    

    【讨论】:

      猜你喜欢
      • 2021-01-22
      • 2021-08-24
      • 1970-01-01
      • 2022-12-23
      • 2020-08-26
      • 2020-08-16
      • 1970-01-01
      • 2011-03-28
      • 1970-01-01
      相关资源
      最近更新 更多