删除除具有特定条件的行以外的所有行

时间:2019-08-01 16:40:01

标签: python pandas

我想删除“文档编号”列中所有不包含“ PD-19-05-16”或“ PD-19-06-01”的行。但是运行代码后,它似乎没有做任何更改。

任何有关如何正确执行此操作或更正代码的建议将不胜感激。

我尝试使用df.drop,但遇到了很多问题。

@app.route('/book/<string:isbn>', methods = ['POST','GET'])
def book(isbn):
    #import columns from database 
    res = db.execute("SELECT * FROM books WHERE isbn = :isbn", {"isbn": isbn}).fetchone()
    #import api from Goodreads (stats) 
    r = requests.get("https://www.goodreads.com/book/review_counts.json", params={"key": "L3FHyOR3IhCo3kctcUz3zg", "isbns": isbn})
    if r.status_code != 200:
      raise ValueError
    reviews_count=r.json()["books"][0]["reviews_count"]
    average_rating=r.json()["books"][0]["average_rating"]
    username = session.get("username")
    if request.method == "POST":
        review = request.form.get("comment")
        rating = request.form.get("rating")
        date = datetime.now()
        user_id = db.execute("SELECT id FROM users WHERE username = :username",{"username":username}).fetchone()
        db.execute("INSERT INTO reviews (user_id, review, rating, date) VALUES (:user_id, :review, :rating, :date)", {"date":date, "review":review, "rating":rating, "user_id":user_id})
        db.commit()
    return render_template("book.html", reviews_count = reviews_count, average_rating = average_rating, email = username)

我希望它能起作用,但出现ValueError:数组的长度不同:4533 vs 2

1 个答案:

答案 0 :(得分:1)

尝试:

rows_to_keep = ["PD-19-05-16","PD-19-06-01"]
df.loc[df['Document No.'].isin(rows_to_keep)] 
#or if you need the inverse
df.loc[~df['Document No.'].isin(rows_to_keep)] 

我认为这不是drop tbf的正确用法。我总是发现使用isin过滤单个列会更容易。

~充当负运算符