Using pandas with Anvil

Hi @ldelfin,

If you want to construct a Pandas data frame out of data held in your Anvil data tables, I’d suggest you construct a list of dicts with the information you want, and then call DataFrame.from_dict() on it.

Eg, if I have a table called “people”, which has name and age columns, plus group which is a link to a row from the “groups” table, and the “groups” table has a column called name, I could do:

from anvil.tables import app_tables
import pandas

# Get an iterable object with all the rows in my_table
all_records = app_tables.people.search()
# For each row, pull out only the data we want to put into pandas
dicts = [{'name': r['name'], 'age': r['age'], 'group_name': r['group']['name']}
         for r in all_records]

df = pandas.DataFrame.from_dict(dicts)

# ...now I have my data as a Pandas data frame!

Does this make sense?


Depending on the amount of data you’re processing, it might take some time to load it all from the data tables, so you might want to run this in an Uplink script to avoid Anvil’s server timeouts.

9 Likes