如何将数组转换为数据帧?

时间:2019-06-26 20:26:27

标签: r

我是新手,我曾尝试将数组转换为数据帧,但是输出是分离的,并且在输出中不显示“ Class:Negative”等。

如何将数组的结果合并到一个考虑了“类别:负数”等的数据帧中?

from django.db import models
from django.urls import reverse
from django.db.models.signals import pre_save, post_save
from .utils import unique_slug_generator, id_generator, curated_slug_generator, poll_slug_generator
from django.contrib.auth.models import User
from django.dispatch import receiver
from datetime import datetime
from django_countries.fields import CountryField

class Movie(models.Model):
    """
    Movie class
    """

    # Fields
    tmdb_id = models.IntegerField()
    title = models.CharField(max_length=100, help_text="Enter title")
    release_date = models.DateField()
    bluray_date = models.DateField(null=True, blank=True)
    poster = models.CharField(max_length=200, help_text="Enter poster file")
    overview = models.TextField(help_text="Enter overview")
    genre = models.ManyToManyField(MovieGenre, help_text="Select a genre for this movie")
    mpaa_rating = models.CharField(max_length=10, help_text="MPAA Rating") #details call
    run_time = models.IntegerField()
    slug = models.SlugField(max_length=100, null=True, blank=True)
    imdb_rating = models.IntegerField(default=60)
    trailer = models.CharField(max_length=100, help_text="Enter trailer link")
    director = models.CharField(max_length=100, blank=True, help_text="Enter director")
    theater_link = models.CharField(max_length=255, null=True, blank=True, help_text="Enter theater link")
    bluray_link = models.CharField(max_length=255, null=True, blank=True, help_text="Enter blu-ray link")
    votes = models.IntegerField(default=0)
    num_ratings = models.IntegerField(default=0)
    running_total = models.IntegerField(default=0)
    weighted_rating = models.IntegerField(default=0)
    active = models.BooleanField(default=False)
    flashback = models.BooleanField(default=False)
    processed = models.BooleanField(default=False)

    # Metadata
    class Meta:
        ordering = ["release_date", "title"]

    # Methods
    def get_absolute_url(self):
        """
        Returns the url to access a particular instance of Movie.
        """
        return reverse('movie-detail', kwargs={'slug': self.slug})

    @property
    def movie_length(self):
        "Returns the movie's length in hours:minutes."
        if self.run_time == 0:
            return None
        else:
            hours = int(self.run_time / 60)
            minutes_in_hrs = hours * 60
            minutes = self.run_time - minutes_in_hrs
            return '%d:%s' % (hours, str(minutes).zfill(2))

    @property
    def format_type(self):
        "Returns the movie's type based on length."
        if not self.run_time:
            return None
        elif self.run_time <= 24:
            return "Short"
        elif self.run_time <= 40:
            return "Featurette"
        else:
            return "Feature"

    @property
    def rating_percent(self):
        if self.imdb_rating == 0:
            return None
        else:
            return str(self.imdb_rating)+"%"

    @property
    def weighted_percent(self):
        if self.weighted_rating == 0:
            return None
        else:
            return str(self.weighted_rating)+"%"

    @property
    def length_interval(self):
        "Returns the movie's length as an interval."
        if self.run_time == 0:
            return None
        else:
            hours = int(self.run_time / 60)
            minutes_in_hrs = hours * 60
            minutes = self.run_time - minutes_in_hrs
            return 'PT%dH%dM' % (hours, minutes)

    def __str__(self):
        """
        String for representing the Movie object (in Admin site etc.)
        """
        return self.title

class Album(models.Model):
    """
    Album class
    """

    # Fields
    spotify_id = models.CharField(max_length=50)
    artist = models.CharField(max_length=100, help_text="Enter artist")
    title = models.CharField(max_length=100, help_text="Enter title")
    image = models.CharField(max_length=100, help_text="Enter image file")
    big_image = models.CharField(max_length=100, help_text="Enter image file")
    release_date = models.DateField()
    record_type = models.CharField(max_length=25, help_text="Enter record type")
    slug = models.SlugField(max_length=100, null=True, blank=True)
    length = models.IntegerField() #in seconds
    link = models.CharField(max_length=255, blank=True, help_text="Enter purchase link")
    genre = models.ManyToManyField(AlbumGenre, blank=True, help_text="Select a genre for this album")
    base_rating = models.IntegerField(default=60)
    votes = models.IntegerField(default=0)
    num_ratings = models.IntegerField(default=0)
    running_total = models.IntegerField(default=0)
    weighted_rating = models.IntegerField(default=0)
    active = models.BooleanField(default=False)
    flashback = models.BooleanField(default=False)
    processed = models.BooleanField(default=False)

    # Metadata
    class Meta:
        ordering = ["release_date", "artist", "title"]

    # Methods
    def get_absolute_url(self):
        """
        Returns the url to access a particular instance of Album.
        """
        return reverse('album-detail', kwargs={'slug': self.slug})

    def __str__(self):
        """
        String for representing the Album object (in Admin site etc.)
        """
        return self.title

    @property
    def rating_percent(self):
        if self.base_rating == 0:
            return None
        else:
            return str(self.base_rating)+"%"

    @property
    def weighted_percent(self):
        if self.weighted_rating == 0:
            return None
        else:
            return str(self.weighted_rating)+"%"

    @property
    def album_length(self):
        "Returns the album's length in minutes:seconds."
        if self.length == 0:
            return None
        else:
            minutes = int(self.length / 60)
            secs_in_mins = minutes * 60
            seconds = self.length - secs_in_mins
            return '%d:%s' % (minutes, str(seconds).zfill(2))

    @property
    def length_interval(self):
        "Returns the album's length as an interval."
        if self.length == 0:
            return None
        else:
            minutes = int(self.length / 60)
            secs_in_mins = minutes * 60
            seconds = self.length - secs_in_mins
            return 'PT%dM%dS' % (minutes, seconds)

class Track(models.Model):
    """
    Track class
    """

    # Fields
    spotify_id = models.CharField(max_length=50)
    title = models.CharField(max_length=100, help_text="Enter title")
    length = models.IntegerField() #in seconds
    track_num = models.IntegerField()
    album = models.ForeignKey(Album, on_delete=models.CASCADE)
    preview = models.CharField(max_length=200, blank=True, help_text="Enter preview url")

    # Metadata
    class Meta:
        ordering = ["album", "track_num"]

    def __str__(self):
        """
        String for representing the Track object (in Admin site etc.)
        """
        return self.title

    @property
    def track_length(self):
        "Returns the track's length in minutes:seconds."
        minutes = int(self.length / 60)
        secs_in_mins = minutes * 60
        seconds = self.length - secs_in_mins
        return '%d:%s' % (minutes, str(seconds).zfill(2))

    @property
    def length_interval(self):
        "Returns the track's length as an interval."
        minutes = int(self.length / 60)
        secs_in_mins = minutes * 60
        seconds = self.length - secs_in_mins
        return 'PT%dM%dS' % (minutes, seconds)

class Book(models.Model):
    """
    Book class
    """

    # Fields
    title = models.CharField(max_length=150, help_text="Enter title")
    cover_image = models.CharField(max_length=200, help_text="Enter image file")
    description = models.TextField(help_text="Enter description")
    author = models.CharField(max_length=100, help_text="Enter author")
    pages = models.IntegerField()
    release_date = models.DateField()
    genre = models.ManyToManyField(BookGenre, help_text="Select a genre for this book")
    slug = models.SlugField(max_length=100, null=True, blank=True)
    amazon_rating = models.IntegerField(default=60)
    votes = models.IntegerField(default=0)
    link = models.CharField(max_length=255, blank=True, help_text="Enter purchase link")
    num_ratings = models.IntegerField(default=0)
    running_total = models.IntegerField(default=0)
    weighted_rating = models.IntegerField(default=0)
    active = models.BooleanField(default=False)
    flashback = models.BooleanField(default=False)
    processed = models.BooleanField(default=False)

    # Metadata
    class Meta:
        ordering = ["author", "title"]

    # Methods
    def get_absolute_url(self):
        """
        Returns the url to access a particular instance of Book.
        """
        return reverse('book-detail', kwargs={'slug': self.slug})

    def __str__(self):
        """
        String for representing the Book object (in Admin site etc.)
        """
        return self.title

    @property
    def rating_percent(self):
        if self.amazon_rating == 0:
            return None
        else:
            return str(self.amazon_rating)+"%"

    @property
    def weighted_percent(self):
        if self.weighted_rating == 0:
            return None
        else:
            return str(self.weighted_rating)+"%"

1 个答案:

答案 0 :(得分:1)

根据显示的示例数据,您没有array,但是没有list可以将其转换为data.frame,可以使用以下代码:

do.call(rbind.data.frame,result2)

如果它不起作用,我将需要一个示例数据。