如何在python中通过另一列的标准过滤时取一列的平均值

时间:2018-07-03 21:44:16

标签: python python-3.x pandas numpy anaconda

我正在Python(通常是R家伙)中工作,我正在尝试为特定的应用程序创建此函数。基本上,我试图将“ Month_of_Year”列中每个月的“ CallsPresented”列取平均值。我知道我正在使这一过程变得比我所需要的更加复杂。我该怎么做?

class CompanyProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
    accountNumber = models.CharField(max_length=25, default=1, blank=False, null=False)
    companyName = models.CharField(max_length=200, blank=False)
    companyRegNum = models.CharField(max_length=30, blank=True)
    contactNumber = models.CharField(max_length=20, blank=False)
    address = models.CharField(max_length=300, blank=True)
    areaCode = models.CharField(max_length=10, blank=False)
    deliveryEmails = models.TextField(blank=True)   #this is the list of all the people chosen to recieve daily notification.
    tenderCategory = models.ManyToManyField(Category, blank=False)    #links the user to the chosen category.
    provinces = models.ManyToManyField(Province, blank=False)    #links the user to the chosen Provinces.
    package = models.ForeignKey(Packages, default=1, blank=False)       #links the user to the chosen package.
    pymntMethod = models.IntegerField(blank=True, default=3)   #this is the chosen payment method (e.g credit card=1, debit order=2 or direct debit=3)
    keywords = models.ManyToManyField(Keywords)     #links the user to the chosen keywords.
    extraKeywords = models.TextField(default='', blank=True)  #this field acts as a container of extra keywords from the user. These are keywords that we do not have in our database.
    contractDuration = models.IntegerField(blank=False, default=12)
    termsAndConditions = models.BooleanField(blank=False, default=1)   #this is the T&C's field that must be agreed to by the client.
    commencementDate = models.DateTimeField(default=timezone.now, blank=True)

    class Keywords(models.Model):
        keyword = models.CharField(max_length=150)

        class Meta:
            verbose_name_plural = ('Keywords')
            ordering = ['keyword', ]

        def __str__(self):
            return self.keyword


    #This is the model that stores the tender.
    class Tender(models.Model):
        tenderCategory = models.ManyToManyField(category, blank=False)       #this field holds the tender category, e.g. construction, engineering, human resources etc.
        tenderProvince = models.ForeignKey(Province, default=1, blank=False)       #this is the province the tender was advertised from.
        keywordTags = models.TextField(blank=False)      #this field holds keywords for the tender as per the tender title or as determined by the tender capturer.
        buyersName = models.CharField(max_length=100)   #this is the name of the Buyer e.g. Dept. of Transport, Transnet, Dept of Agriculture etc.
        summary = models.TextField(blank=False)      #this is the tender title as per the Buyer.
        refNum = models.CharField(max_length=100)    #tender ref number as per the Buyer.
        issueDate = models.DateTimeField(blank=True, null=True)     #date the tender was published
        closingDate = models.DateTimeField(blank=True, null=True)   #tender closing date
        siteInspection = models.TextField(blank=True, null=True)     #site inspection date, if any
        enquiries = models.TextField(blank=True, null=True) #this field stores details of the contact person, for the tender.
        description = models.TextField(blank=True, null=True)   #this is the body of the tender. the tender details are captured here.
        assigned_keywords = models.ManyToManyField(Keywords, blank=True, through='tenderKeywords')
        matched = models.BooleanField(default=0, blank=False)
        capture_date = models.DateField(default=timezone.now, blank=False, null=False)


    class TendersKeywords(models.Model):
        tender = models.ForeignKey(tender, related_name='tender_keywords')
        keyword = models.ForeignKey(Keywords, related_name='tender_keywords')
        date_assigned = models.DateField(default=timezone.now, blank=False, null=False)

1 个答案:

答案 0 :(得分:1)

为什么不仅仅groupBy月份列并为每个组计算mean

类似

def get_monthly_mean(df):

    df_grouped = df.groupby('Month_of_Year')['CallsPresented'].mean()

    #Then you can pass the column to a list or just return the grouped df, 
    #whatever suits your use case better

    return df_grouped