Skip to content
Advertisement

Creating a single-column dataframe in Pandas from a Django queryset

I’m trying to create a dataframe containing the values from field_1 and field_2 in a single column. I haven’t used pandas a whole lot before, so I’m sure this is naive.

JavaScript

If I’m working with a fairly large dataset, is there a way I can make this more efficient? I would like to eliminate the step that creates the CSV entirely, if possible. If anybody can point me in the right direction, that’d be fantastic.

Advertisement

Answer

You really don’t need:

  • the order objects and getattr; use .values_list() to get an iterable of 2-tuples (assuming field_names are actual fields on the model).
  • CSV – now that you have an iterable of 2-tuples, pass them to the DataFrame constructor along with the respective column names.
JavaScript
  • Necessarily even Pandas
JavaScript

and you have a list of description, comments strings, just like the series you’d get with Pandas.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement