【问题标题】:Pandas dataframe If else with logical AND involving two columns熊猫数据框如果其他逻辑与涉及两列
【发布时间】:2016-08-18 10:55:08
【问题描述】:

如何在涉及熊猫数据框两列的控制语句中添加逻辑AND,即

这行得通:

def getContinent(row):
    if row['Location'] in ['US','Canada']:
        val = 'North America'
    elif row['Location'] in['UK', 'Germany']:
        val = 'Europe'
    else:
        val = None
    return val

df.apply(getContinent, axis=1)

现在我想在另一个字段 row['Sales'] 中包含一个附加条件:

def getContinent(row):
    if row['Location'] in ['US','Canada'] & row['Sales'] >= 100:
        val = 'North America'
    elif row['Location'] in['UK', 'Germany'] & row['Sales'] < 100:
        val = 'Europe'
    else:
        val = None
    return val

df.apply(getContinent, axis=1)

ValueError: ('Arrays were different lengths: 6132 vs 2', u'occurred at index 0')

【问题讨论】:

  • and而不是&amp;

标签: python pandas if-statement dataframe apply


【解决方案1】:

您需要使用and 而不是&amp;

df = pd.DataFrame({'Sales': {0: 400, 1: 20, 2: 300}, 
                   'Location': {0: 'US', 1: 'UK', 2: 'Slovakia'}})
print (df)

   Location  Sales
0        US    400
1        UK     20
2  Slovakia    300

def getContinent(row):
    if row['Location'] in ['US','Canada'] and row['Sales'] >= 100:
        val = 'North America'
    elif row['Location'] in['UK', 'Germany'] and row['Sales'] < 100:
        val = 'Europe'
    else:
        val = None
    return val

print (df.apply(getContinent, axis=1))
0    North America
1           Europe
2             None
dtype: object

【讨论】:

  • 我收到了这个错误:ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', u'occurred at index 0')
  • 我无法模拟问题,你能添加DataFrame的样本吗?
猜你喜欢
  • 2019-06-06
  • 1970-01-01
  • 2019-12-09
  • 1970-01-01
  • 1970-01-01
  • 2020-10-21
  • 1970-01-01
  • 2017-09-09
  • 1970-01-01
相关资源
最近更新 更多