小编ele*_*tiv的帖子

如何初始化 peewee.Model 对象?__init__() 不起作用

我无法使用通常的init () 方法初始化“peewee.Model”后代对象的字段。我该如何初始化?

import peewee

peewee_database = peewee.SqliteDatabase('example.db')

class Config():

    def __init__(self, seats, cylinders):
        self.seats = seats
        self.cylinders = cylinders

class Car(peewee.Model):

    magic_number = peewee.IntegerField()
    color = peewee.TextField()

    class Meta:
        database = peewee_database

    def __init__(self, config):
        self.magic_number = config.seats / config.cylinders
        self.color = None

peewee_database.connect()
peewee_database.create_tables([Car])

config = Config(7, 6)
car = Car(config)
car.color = "blue"
car.save()
Run Code Online (Sandbox Code Playgroud)

在 Python3 中产生此错误:

  File "test.py", line 27, in <module>
    car = Car(config)
  File "test.py", line 20, in __init__
    self.magic_number = config.seats / …
Run Code Online (Sandbox Code Playgroud)

python peewee

5
推荐指数
1
解决办法
1489
查看次数

Pandas:按分位数分组并计算统计数据

我有99人的年收入数据:

import pandas, random
incomes = pandas.DataFrame({'income':[round(random.triangular(20,80,200),0) for i in range(99)]}) 
Run Code Online (Sandbox Code Playgroud)

如何:

  • 将他们分为 3 个分位数,“贫困”、“中等”、“富裕”,每组 33 人
  • 计算每个分位数的平均收入

抱歉,听起来像是一个新手问题。我在学。谢谢你!

python grouping quantile pandas

2
推荐指数
1
解决办法
4253
查看次数

标签 统计

python ×2

grouping ×1

pandas ×1

peewee ×1

quantile ×1