如何在pandas(Python)中评估类别而不是书籍?

时间:2018-04-04 15:01:04

标签: python pandas dataframe

我完全重新阐述了我的问题。我很确定现在更有道理。

我有以下条件:logic1 & (logic2 | logic3) & logic4Category的所有图书都应标记为Evaluated,一旦至少有一本书满足条件。我怎样才能为每个CategoryID执行此操作?

逻辑类别评估

logic1 = (books['CategoryPopulair'] == True)
logic2 = (books['BookTitle'] == 'Hello1') & (books['BookRead'] == True) & (books['BookNice'] == True)
logic3 = (books['BookTitle'].isin(['Hello2', 'Hello3'])) & (books['BookNice'] == True) & (books['BookOld'] == True)
logic4 = (books['BookYear'].isin([393, 1962, 1964, 3295]))

输入

| CategoryID | BookID | BookStatus | BookTitle | BookRead | BookNice | BookOld | BookYear | CategoryPopulair |
|------------|--------|------------|-----------|----------|----------|---------|----------|------------------|
| 337830     | 1      | 4          | Hello1    | TRUE     | TRUE     | TRUE    | 1964     | TRUE             |
| 337830     | 2      | 4          | Hello2    |          | TRUE     | TRUE    | 1964     | TRUE             |
| 337830     | 3      | 1          | Hello3    |          | TRUE     | TRUE    | 1964     | TRUE             |
| 337830     | 4      | 1          | Hello4    |          | TRUE     | TRUE    | 1964     | TRUE             |
| 337830     | 5      | 1          | Hello5    |          | TRUE     | FALSE   | 1964     | TRUE             |
| 337830     | 6      | 1          | Hello6    |          | TRUE     | FALSE   | 1964     | TRUE             |
| 337830     | 7      | 1          | Hello7    |          | TRUE     | TRUE    | 1964     | TRUE             |
| 337830     | 8      | 1          | Hello8    |          | TRUE     | FALSE   | 1964     | TRUE             |
| 337830     | 9      | 1          | Hello9    |          | TRUE     | TRUE    | 1964     | TRUE             |
| 337830     | 10     | 1          | Hello10   |          | TRUE     | FALSE   | 1964     | TRUE             |
| 337830     | 11     | 1          | Hello11   |          | TRUE     | TRUE    | 1964     | TRUE             |
| 337830     | 12     | 1          | Hello12   |          | TRUE     | TRUE    | 1964     | TRUE             |

| 331232     | 1      | 4          | Hello1    | TRUE     | TRUE     | TRUE    | 1964     | TRUE             |
| 331232     | 2      | 4          | Hello2    | TRUE     | TRUE     | TRUE    | 1964     | TRUE             |
| 331232     | 13     | 1          | Hello13   |          | TRUE     | TRUE    | 1964     | TRUE             |
| 331232     | 14     | 1          | Hello14   |          | TRUE     | TRUE    | 1964     | TRUE             |
| 331232     | 5      | 1          | Hello5    |          | TRUE     | FALSE   | 1964     | TRUE             |
| 331232     | 6      | 1          | Hello6    |          | TRUE     | FALSE   | 1964     | TRUE             |
| 331232     | 15     | 1          | Hello15   |          | TRUE     | TRUE    | 1964     | TRUE             |
| 331232     | 8      | 1          | Hello8    |          | TRUE     | FALSE   | 1964     | TRUE             |
| 331232     | 9      | 1          | Hello9    |  TRUE    | TRUE     | TRUE    | 1964     | TRUE             |
| 331232     | 10     | 1          | Hello10   |          | TRUE     | FALSE   | 1964     | TRUE             |
| 331232     | 17     | 1          | Hello17   |  TRUE    | TRUE     | TRUE    | 1964     | TRUE             |
| 331232     | 16     | 1          | Hello16   |          | TRUE     | TRUE    | 1964     | TRUE             |

我需要的输出(请参阅CategoryEvaluated列)

BookOld 2 BookIDCategoryIDCategoryEvaluated 331232错误导致| CategoryID | BookID | BookStatus | BookTitle | BookRead | BookNice | BookOld | BookYear | CategoryPopulair | CategoryEvaluated | |------------|--------|------------|-----------|----------|----------|---------|----------|------------------|-------------------| | 337830 | 1 | 4 | Hello1 | TRUE | TRUE | TRUE | 1964 | TRUE | TRUE | | 337830 | 2 | 4 | Hello2 | | TRUE | TRUE | 1964 | TRUE | TRUE | | 337830 | 3 | 1 | Hello3 | | TRUE | TRUE | 1964 | TRUE | TRUE | | 337830 | 4 | 1 | Hello4 | | TRUE | TRUE | 1964 | TRUE | TRUE | | 337830 | 5 | 1 | Hello5 | | TRUE | FALSE | 1964 | TRUE | TRUE | | 337830 | 6 | 1 | Hello6 | | TRUE | FALSE | 1964 | TRUE | TRUE | | 337830 | 7 | 1 | Hello7 | | TRUE | TRUE | 1964 | TRUE | TRUE | | 337830 | 8 | 1 | Hello8 | | TRUE | FALSE | 1964 | TRUE | TRUE | | 337830 | 9 | 1 | Hello9 | | TRUE | TRUE | 1964 | TRUE | TRUE | | 337830 | 10 | 1 | Hello10 | | TRUE | FALSE | 1964 | TRUE | TRUE | | 337830 | 11 | 1 | Hello11 | | TRUE | TRUE | 1964 | TRUE | TRUE | | 337830 | 12 | 1 | Hello12 | | TRUE | TRUE | 1964 | TRUE | TRUE | | 331232 | 1 | 4 | Hello1 | TRUE | TRUE | TRUE | 1964 | TRUE | FALSE | | 331232 | 2 | 4 | Hello2 | TRUE | TRUE |->FALSE<-| 1964 | TRUE | FALSE | | 331232 | 13 | 1 | Hello13 | | TRUE | TRUE | 1964 | TRUE | FALSE | | 331232 | 14 | 1 | Hello14 | | TRUE | TRUE | 1964 | TRUE | FALSE | | 331232 | 5 | 1 | Hello5 | | TRUE | FALSE | 1964 | TRUE | FALSE | | 331232 | 6 | 1 | Hello6 | | TRUE | FALSE | 1964 | TRUE | FALSE | | 331232 | 15 | 1 | Hello15 | | TRUE | TRUE | 1964 | TRUE | FALSE | | 331232 | 8 | 1 | Hello8 | | TRUE | FALSE | 1964 | TRUE | FALSE | | 331232 | 9 | 1 | Hello9 | TRUE | TRUE | TRUE | 1964 | TRUE | FALSE | | 331232 | 10 | 1 | Hello10 | | TRUE | FALSE | 1964 | TRUE | FALSE | | 331232 | 17 | 1 | Hello17 | TRUE | TRUE | TRUE | 1964 | TRUE | FALSE | | 331232 | 16 | 1 | Hello16 | | TRUE | TRUE | 1964 | TRUE | FALSE | 为假。

from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import sessionmaker

engine = create_engine('sqlite:///')
Session = sessionmaker(bind=engine)
sess = Session()
Base = declarative_base()

class Users(Base):
    __tablename__ = 'mans'
    id = Column(Integer, primary_key=True)
    user_id = Column(Integer, ForeignKey("address.id"))
    name = Column(String)
    age = Column(Integer)

class Address(Base):
    __tablename__ = 'address'
    id = Column(Integer, primary_key=True)
    email = Column(String)

Base.metadata.create_all(engine)

user1 = Users(id=1, user_id=2, name='Mike', age=21)
adr1 = Address(id=1, email='test@mail.ru')


sess.add(user1)
sess.add(adr1)

sess.commit()
x = sess.query(Users).filter(Users.id>0).first()

print(x.name)

1 个答案:

答案 0 :(得分:1)

已编辑以更好地回答您的问题。

因为any()可能有点棘手,我倾向于创建一个布尔列,然后评估每个组。你将按照每本书的所有逻辑,然后检查是否有任何书符合标准。

books['FitsLogic'] = logic1 & (logic2 | logic3) & logic4
books['CategoryEvaluted'] = books['FitsLogic'].groupby(books['CategoryID']).transform(
                               lambda x: True if any(x) else False)