我不明白为什么我的if循环无法正常工作

时间:2018-12-15 12:38:49

标签: python pandas dataframe if-statement

我写了下一个周期,但是当我运行它时,Spyder向我显示了下一条消息:

  

ValueError:系列的真值不明确。使用空   a.bool(),a.item(),a.any()或a.all()。

为什么会这样?因为我指定了在不满足任何条件的情况下要分配给该列的值。

  func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {



    let kCellActionWidth = CGFloat(70.0)// The width you want of delete button
    let kCellHeight = tableView.frame.size.height // The height you want of delete button
    let whitespace = whitespaceString(width: kCellActionWidth) // add the padding


    let deleteAction = UITableViewRowAction(style: .`default`, title: "") {_,_ in
        // do whatever the action you want
    }

    // create a color from patter image and set the color as a background color of action
    let view = UIView(frame: CGRect(x: tableView.frame.size.width-70, y: 0, width: 70, height: kCellHeight))
    view.backgroundColor = UIColor(red: 255/255, green: 48/255, blue: 32/255, alpha: 1)
    let imageView = UIImageView(frame: CGRect(x: 25,
                                              y: 20,
                                              width: 30,
                                              height: 30))

    imageView.image = UIImage(named: "trash-filled")! // required image
    view.addSubview(imageView)
    let image = view.image()

    deleteAction.backgroundColor = UIColor.init(patternImage: image)

    let muteAction = UITableViewRowAction(style: .`default`, title: "") {_,_ in
        // do whatever the action you want
        self.isMuteNotifications = !self.isMuteNotifications

    }

    // create a color from patter image and set the color as a background color of action
    let view1 = UIView(frame: CGRect(x: tableView.frame.size.width-70, y: 0, width: 70, height: kCellHeight))
    view1.backgroundColor = UIColor(red: 238/255, green: 142/255, blue: 48/255, alpha: 1)
    let imageView1 = UIImageView(frame: CGRect(x: 25,
                                               y: 20,
                                               width: 30,
                                               height: 30))
    if isMuteNotifications {
        imageView1.image = UIImage(named: "mute")! // required image
    }
    else {
        imageView1.image = UIImage(named: "unmute")! // required image
    }

    view1.addSubview(imageView1)
    let image1 = view1.image()

    muteAction.backgroundColor = UIColor.init(patternImage: image1)

    return [deleteAction,muteAction]

}

3 个答案:

答案 0 :(得分:1)

因为df['age']在熊猫中指定了一个Series类型,并且它不是一个单一的值,所以您不能简单地编写df['age'] <= 20并且它可以是任何值或所有值,因此您可以简单地使用df['age'].all() <= 20

要解决您的问题,可以使用熊猫过滤,如下所示:

df_part = df[(df['age'] <= 20) & (df['age' >= 11)]
df_part['age_enc'] = 20

然后您可以将这些数据框部分合并在一起

答案 1 :(得分:0)

两件事在这里出了问题。首先,您必须拆分比较。其次,这种比较将产生一个布尔型ndarray,您不能直接将其用作条件,因为它是不明确的。您可以这样做:

if (11 <= df['age']).all() and (df['age'] <= 20).all():
    ...

那太冗长了,但是这行不通

if (11 <= df['age'] <= 20).all():
    ...

请注意,我将>=更改为<=,您可以使用anyall,视情况而定。让我知道这是否对您有用。

答案 2 :(得分:0)

我认为我找到了最简单的解决方案:

# encoding
from sklearn import preprocessing
le1 = preprocessing.LabelEncoder()
le1.fit(df['age'])
df['age_enc'] = le1.transform(df['age'])
#
keys = le1.classes_
values = le1.transform(le1.classes_)
dictionary = dict(zip(keys, values))
print(dictionary)

因为我想对变量进行转码并创建一个新列-这种方式可能是最简单的