Comments (5)
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.
Same for reverse foreign key relations.
from django-pandas.
@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.
@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.
Nice, @chrisdev! Thank you so much for explaining this!
from django-pandas.
Related Issues (20)
- verbose does not return Foreign Key object's name
- Cannot extend admin-tools blocks into new templates HOT 1
- Django 3.1: AttributeError: module 'django.db.models.fields' has no attribute 'FieldDoesNotExist' with annotation HOT 3
- Safely saving pandas DataFrame to SQL table via Django HOT 5
- read_frame bug: ForeignKey lookup fails if any Null values present HOT 2
- Possible to release latest dev version to PyPI? HOT 8
- Bug: `read_frame` does not work if at least one manager is not named "objects"
- How can I merge different tables on the basis of a foreign key?
- error if primary_key is not int() HOT 6
- Converting django.models.DateField to datetime64 in read_frame
- test_io.RelatedFieldsTest.test_verbose: ValueError: cannot convert float NaN to integer HOT 6
- Race condition of queryset in read_frame HOT 3
- configure github actions (travis tests are no longer running) HOT 4
- run tests with warnings enabled HOT 1
- Stack Trace - ValueError: cannot convert float NaN to integer - When a foreign key is null HOT 4
- force_text removed in Django 4.0 HOT 2
- Bug with binary field and to_pivot_table HOT 2
- Data truncation
- read_frame breaks if fieldnames and column_names are passed and there is an FK reference in the fieldnames
- Pandas 2.1 has a deprecation warning for convert_dtype on read_frame HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-pandas.