使用django模型将{pandas df转移到db.sqlite3

时间:2017-11-28 12:18:33

标签: python django pandas sqlite python-3.6

我的dfs包含以下列标题:

Year JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC WIN SPR SUM AUT ANN ATTRIBUTE LOCATION

每个列都包含随机浮点值,但ATTRIBUTE除外,它包含此列表中的值:

[Max_temp, Min_temp, Mean_temp, Sunshine, Rainfall]
  • 和LOCATION,其中包含此列表中的值:

    [英国,英格兰,苏格兰,威尔士]

我需要使用以下模型将它放入django附带的sqlite3数据库中:

from django.db import models
from django.core.validators import MaxValueValidator, MinValueValidator


class Location(models.Model):
    LOCATIONS = (
        ('EN', 'England'),
        ('SC', 'Scotland'),
        ('WA', 'Wales'),
        ('UK', 'United Kingdom'),
    )
    location = models.CharField(max_length=2, choices=LOCATIONS)

    class Meta:
        verbose_name_plural = "Location"

    def __str__(self):
        return self.location

class Max_temp(models.Model):
    location = models.ForeignKey(Location, on_delete=models.CASCADE)
    year = models.IntegerField(
    default=0,
    validators=[MaxValueValidator(9999), MinValueValidator(0)]
)
    MONTH_OR_SEASON = (
        ("JAN", "January"),
        ("FEB", "February"),
        ("MAR", "March"),
        ("APR", "April"),
        ("MAY", "May"),
        ("JUN", "June"),
        ("JUL", "July"),
        ("AUG", "August"),
        ("SEP", "September"),
        ("OCT", "October"),
        ("NOV", "November"),
        ("DEC", "December"),
        ("WIN", "Winter"),
        ("SPR", "Spring"),
        ("SUM", "Summer"),
        ("AUT", "Autumn"),
        ("ANN", "Annual"),
)
    month_or_season = models.CharField(max_length=3, choices=MONTH_OR_SEASON)

    class Meta:
        verbose_name_plural = "Maximum Temperature"

    def __str__(self):
        return self.year

class Min_temp(models.Model):
    location = models.ForeignKey(Location, on_delete=models.CASCADE)
    year = models.IntegerField(
    default=0,
    validators=[MaxValueValidator(9999), MinValueValidator(0)]
)
    MONTH_OR_SEASON = (
        ("JAN", "January"),
        ("FEB", "February"),
        ("MAR", "March"),
        ("APR", "April"),
        ("MAY", "May"),
        ("JUN", "June"),
        ("JUL", "July"),
        ("AUG", "August"),
        ("SEP", "September"),
        ("OCT", "October"),
        ("NOV", "November"),
        ("DEC", "December"),
        ("WIN", "Winter"),
        ("SPR", "Spring"),
        ("SUM", "Summer"),
        ("AUT", "Autumn"),
        ("ANN", "Annual"),
)
    month_or_season = models.CharField(max_length=3, choices=MONTH_OR_SEASON)

    class Meta:
        verbose_name_plural = "Minimum Temperature"

    def __str__(self):
        return self.year

class Mean_temp(models.Model):
    location = models.ForeignKey(Location, on_delete=models.CASCADE)
    year = models.IntegerField(
    default=0,
    validators=[MaxValueValidator(9999), MinValueValidator(0)]
)
    MONTH_OR_SEASON = (
        ("JAN", "January"),
        ("FEB", "February"),
        ("MAR", "March"),
        ("APR", "April"),
        ("MAY", "May"),
        ("JUN", "June"),
        ("JUL", "July"),
        ("AUG", "August"),
        ("SEP", "September"),
        ("OCT", "October"),
        ("NOV", "November"),
        ("DEC", "December"),
        ("WIN", "Winter"),
        ("SPR", "Spring"),
        ("SUM", "Summer"),
        ("AUT", "Autumn"),
        ("ANN", "Annual"),
)
    month_or_season = models.CharField(max_length=3, choices=MONTH_OR_SEASON)

    class Meta:
        verbose_name_plural = "Mean Temperature"

    def __str__(self):
        return self.year

class Sunshine(models.Model):
    location = models.ForeignKey(Location, on_delete=models.CASCADE)
    year = models.IntegerField(
    default=0,
    validators=[MaxValueValidator(9999), MinValueValidator(0)]
)
    MONTH_OR_SEASON = (
        ("JAN", "January"),
        ("FEB", "February"),
        ("MAR", "March"),
        ("APR", "April"),
        ("MAY", "May"),
        ("JUN", "June"),
        ("JUL", "July"),
        ("AUG", "August"),
        ("SEP", "September"),
        ("OCT", "October"),
        ("NOV", "November"),
        ("DEC", "December"),
        ("WIN", "Winter"),
        ("SPR", "Spring"),
        ("SUM", "Summer"),
        ("AUT", "Autumn"),
        ("ANN", "Annual"),
)
    month_or_season = models.CharField(max_length=3, choices=MONTH_OR_SEASON)

    class Meta:
        verbose_name_plural = "Sunshine"

    def __str__(self):
        return self.year

class Rainfall(models.Model):
    location = models.ForeignKey(Location, on_delete=models.CASCADE)
    year = models.IntegerField(
    default=0,
    validators=[MaxValueValidator(9999), MinValueValidator(0)]
)
    MONTH_OR_SEASON = (
        ("JAN", "January"),
        ("FEB", "February"),
        ("MAR", "March"),
        ("APR", "April"),
        ("MAY", "May"),
        ("JUN", "June"),
        ("JUL", "July"),
        ("AUG", "August"),
        ("SEP", "September"),
        ("OCT", "October"),
        ("NOV", "November"),
        ("DEC", "December"),
        ("WIN", "Winter"),
        ("SPR", "Spring"),
        ("SUM", "Summer"),
        ("AUT", "Autumn"),
        ("ANN", "Annual"),
)
    month_or_season = models.CharField(max_length=3, choices=MONTH_OR_SEASON)

    class Meta:
        verbose_name_plural = "Rainfall"

    def __str__(self):
        return self.year

我对如何做到这一点感到有点难过 - 我不确定我的模型结构是否正确?有没有办法构建模型,所以我不必重复代码?我需要一个像这样结构化的数据库:

  

位置 - Max_temp
            - Min_temp
            - Mean_temp
            - 阳光
            - 降雨

所以每个位置都有一个Max_temp表,一个Min_temp表等......

主要的是,我如何告诉Django将pandas df放入数据库以及Django项目中我放置代码的位置?

任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:2)

你对模型结构提出质疑是正确的:)通常当你有那么多代码重复时,有一种更简单的方法。关键在于您的测量都具有相同的模式。您定义的每个模型都代表一个数据库表,我们实际上不需要为每个测量类型创建一个新的数据库表 - 我们只需将该类型存储为“测量”表中的列。

我还质疑你是否需要Location的单独表格,因为它包含的是国家代码的详细名称 - 你也可以使用ChoiceField(如果你的用例比然后随意将位置模型分开)。

from django.db import models
from django.core.validators import MaxValueValidator, MinValueValidator


class Measurement(models.Model):
    MEASUREMENT_TYPES = (
        ("max_temp", "Maximum Temperature"),
        ("min_temp", "Minimum Temperature"),
        ("mean_temp", "Mean Temperature"),
        ("sunshine", "Sunshine"),
        ("rainfall", "Rainfall"),
    )

    LOCATIONS = (
        ('EN', 'England'),
        ('SC', 'Scotland'),
        ('WA', 'Wales'),
        ('UK', 'United Kingdom'),
    )

    MONTH_OR_SEASON = (
        ("JAN", "January"),
        ("FEB", "February"),
        ("MAR", "March"),
        ("APR", "April"),
        ("MAY", "May"),
        ("JUN", "June"),
        ("JUL", "July"),
        ("AUG", "August"),
        ("SEP", "September"),
        ("OCT", "October"),
        ("NOV", "November"),
        ("DEC", "December"),
        ("WIN", "Winter"),
        ("SPR", "Spring"),
        ("SUM", "Summer"),
        ("AUT", "Autumn"),
        ("ANN", "Annual"),
    )

    measurement_type = models.CharField(
        max_length=255, choices=MEASUREMENT_TYPES, default="max_temp",
    )
    location = models.CharField(
        max_length=2, choices=LOCATIONS, default="EN",
    )
    year = models.IntegerField(
        default=0,
        validators=[MaxValueValidator(9999), MinValueValidator(0)],
    )
    month_or_season = models.CharField(
        max_length=3, choices=MONTH_OR_SEASON, default="ANN",
    )
    value = models.FloatField(
        default=0,
        validators=[MinValueValidator(0)],
    )

    def __str__(self):
        return "{0} in {1} - {2}, {3}".format(
            self.measurement_type, self.location,
            self.month_or_season, self.year, 
        )

现在当您遍历数据框中的每一行时,您会获得ATTRIBUTE值(您可以将其转换为.lower()以符合您在MEASUREMENT_TYPES中定义的models.py选项LOCATION)和JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC WIN SPR SUM AUT ANN以及Measurement中的每个值,您可以使用该属性,位置和月份/季节以及该列中的值创建新的Measurement。< / p>

要创建新的for row in my_data_frame: attribute = row['ATTRIBUTE'] # (or whatever) location = row['LOCATION'] ... for month_or_season in MONTH_AND_SEASONS: value = row[month_or_season] .... Measurement( measurement_type=attribute, location=location, value=value, ... ).save() # Saves model instance to database 对象,您可以这样做:

c = [11, 10, 8, 10, 13, 10]
n = sum(c)