Add_row to data table

Hi. I was wondering what the add_row function can take instead of passing in column name = row value directly? Can it take a list or dictionary with these in?

This is to help me to populate a data table from a dictionary containing key value pairs where the values are lists. The first item in each list should go in the first row, the second item in the second row etc. Each key is a different column. Thanks.

1 Like

Absolutely to this part of the question.

You can do:

my_dict={'my_column_1': 'my_value_1', 'my_column_2': 'my_value_2'}
app_tables.my_table.add_row(**my_dict)

This is assuming you have those columns in your data table (or you have “auto-create missing columns when adding rows checked”).

If you have a single dictionary with lists as values, perhaps you could convert to a list of dictionaries and then add rows using the code above in a loop.

For example, if you have Pandas:

import pandas as pd

df=pd.DataFrame(my_dict)
list_of_dicts=df.to_dict('records') # converting to list of dicts

for d in list_of_dicts:
    app_tables.my_table.add_row(**d)

A non-Pandas way to get to a list of dicts could be:

list_of_dicts = [dict(zip(my_dict,v)) for v in zip(*my_dict.values())]

Perhaps others know a better way of adding rows to a DataTable given your initial data structure, but this is how I’ve done it in the past.

7 Likes

A non-Pandas way to get to a list of dicts could be:
list_of_dicts = [dict(zip(my_dict,v)) for v in zip(*my_dict.values())]

That is a nice piece of Python!

For anybody not sure what it does, it converts this:

my_dict = {
  "col1": [1, 2, 3],
  "col2": ['a', 'b', 'c'],
  "col3": ['foo', 'bar', 'baz'],
}

Into this:

[
  {'col1': 1, 'col2': 'a', 'col3': 'foo'},
  {'col1': 2, 'col2': 'b', 'col3': 'bar'},
  {'col1': 3, 'col2': 'c', 'col3': 'baz'}
]

Which you can use to create 3 Data Tables rows for a table with columns col1, col2, and col3:

for row in list_of_dicts:
  app_tables.my_table.add_row(**row)
6 Likes

Hey nice to hear from you @alcampopiano and thank you for your answer! Just a few comments as you have done some things here I haven’t seen before and have just now been learning about.

First of all the whole ** thing is mind blowing, and was a big piece of the answer is hadn’t been able to work out for myself.

And second, using the string “records” in the to_dict function to give me the dictionary structured in the right way in the first place. I have since seen there are many other ways to change how the dictionary is laid out when going from pandas dataframe to dictionary.

So basically a huge thanks for also helping me to learn two new things with data wrangling :blush:

2 Likes

@shaun thank you too for your extra explanation which helped me too. It is easy now to see how this results in something which will work in anvils add_row function

I learnt something here, too. Although it took @shaun’s explanation for me to get it :slight_smile:

1 Like

All credit goes to Stack Overflow for that nice line. It’s a fun one to break apart and analyze.

Although, my Googling skills are indeed blinding.

2 Likes