Python / Pandas条件新列ValueError

时间:2018-10-22 01:53:02

标签: python-3.x pandas

我希望将新列添加到现有的DataFrame中,并根据df.TaxStatus(1 | 2)的值将df.Public设置为True或False。我看到其他人使用Numpy.where()创建了一个新列,但出现了ValueError。

df['TaxStatus'] = np.where(df.Public == 1, True, np.where(df.Public == 2, False))

这似乎可行,除了ValueError:either both or neither of x and y should be given

1 个答案:

答案 0 :(得分:0)

您可以简单地说:

df.Public == 1

表达式np.where()返回可以分配给新列的一系列True / False值。无需 { "_id" : ObjectId("5bccf4d7ac8fa95f43a964c2"), "KITE" : { "registration" : "DTH498" } } { "_id" : ObjectId("5bccf4d7ac8fa95f43a964c3"), "KITE" : { "registration" : "HKJ607" } } { "_id" : ObjectId("5bccf4d7ac8fa95f43a964c4"), "KITE" : { "registration" : "GCF21" } } { "_id" : ObjectId("5bccf4d7ac8fa95f43a964cf"), "PERSON" : { "name" : "H.Y", "used" : [ { "registration" : "DTH498" }, { "registration" : "HKJ607" }, { "registration" : "GCF21" } ] } } { "_id" : ObjectId("5bccf4d7ac8fa95f43a9leo5"), "PERSON" : { "name" : "T.G", "used" : [ { "registration" : "DTH498" }, { "registration" : "HKJ607" }, { "registration" : "GCF21" } ] } } { "_id" : ObjectId("5bccf4d7ac8fro4943a01pak"), "PERSON" : { "name" : "X.L", "used" : [ { "registration" : "DTH498" }, { "registration" : "HKJ607" }, { "registration" : "GCF21" } ] } }