GithubHelp home page GithubHelp logo

Comments (5)

paulochf avatar paulochf commented on July 16, 2024

I think I'm having the same problem.

I have models like

class Category(models.Model):
    name    = models.CharField()
    showing = models.BooleanField(default=False)

    objects = DataFrameManager()

class Food(models.Model):
    name    = models.CharField()

    objects = DataFrameManager()

class FoodCategory(models.Model):
    food     = models.ForeignKey('Food')
    category = models.ForeignKey('Category')
    primary  = models.BooleanField(default=False)

    objects  = DataFrameManager()

Then, I try

foods = FoodCategory.objects.only("food__name").filter(category__showing=True)
df = foods.to_dataframe()

df.head()

which prints

id  food category    is_primary
0   1   Food object    Category object   True
1   2   Food object    Category object   True
2   4   Food object    Category object   True
3   5   Food object    Category object   True
4   6   Food object    Category object   True

and then I cannot/don't know how to access the inner object.

df.loc[:,"food"]

0     Food object
1     Food object
2     Food object
3     Food object
...

How to access Food name attribute?

Thanks!

from django-pandas.

bufke avatar bufke commented on July 16, 2024

Same for reverse foreign key relations.

from django-pandas.

chrisdev avatar chrisdev commented on July 16, 2024

@evdoks sorry for the late response. You are correct!! I'll fix in in the next release. In the mean time here is a workaround. Using pandas and values_list QuerySets

import pandas as pd
pd.DataFrame.from_records(list(Pizza.objects.all().values_list('name', 'toppings__name')))

from django-pandas.

chrisdev avatar chrisdev commented on July 16, 2024

@paulochf @bufke again sorry for the very tardy response.
I'm working on an IPython notebook/blog post with some examples of usage.

df = FoodCategory.objects.only("food__name").filter(category__showing=False).to_dataframe()
# Show the frame to see the column names 
df.head()

#to get the food column you specify a list with the column names that you want ['food']
df[['food']]
#to get food and primary
df[['food, 'primary']]

Note, you must define the __str__ method in your Models for the verbose resolution to work
That's why in your example above you have Food Object in the rows of your pandas DataFrame.

@python_2_unicode_compatible
class Category(models.Model):
    name = models.CharField(max_length=200)
    showing = models.BooleanField(default=False)

    objects = DataFrameManager()
    def __str__(self):
        return "{0}-{1}".format(
            self.name,
            self.showing
        )

@python_2_unicode_compatible
class Food(models.Model):
    name = models.CharField(max_length=200)

    objects = DataFrameManager()

    def __str__(self):
        return "{}".format(self.name)

Also you can get a DateFrame with exactly the columns you want by specifying them in a list as the first or fieldnames argument in the to_dataframe method. You can use __ for relationship spanning

FoodCategory.objects.filter(category__showing=False).to_dataframe(
    ['category__name', 'food__name', 'primary']
)

from django-pandas.

paulochf avatar paulochf commented on July 16, 2024

Nice, @chrisdev! Thank you so much for explaining this!

from django-pandas.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.