Python Forum
Automating PyTables Dataset Creation and Append - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Automating PyTables Dataset Creation and Append (/thread-30352.html)



Automating PyTables Dataset Creation and Append - Robotguy - Oct-16-2020

In my script, I create several datasets manually:

        import tables
        dset1 = f.create_earray(f.root, "dataset1", atom=tables.Float64Atom(), shape=(0, 2))
        dset2 = f.create_earray(f.root, "dataset2", atom=tables.Float64Atom(), shape=(0, 2))
        dset3 = f.create_earray(f.root, "dataset3", atom=tables.Float64Atom(), shape=(0, 2))
        ...
I want to achieve two things:
1) Automate the above statements to execute in a loop fashion and create any desired (N) datasets
2) Then I also use .append method sequentially (as given below) which I also want to automate:

        dset1.append(np_array1) 
        dset2.append(np_array2) 
        dset3.append(np_array3) 
        ...
Will appreciate any assistance?


RE: Automating PyTables Dataset Creation and Append - jefsummers - Oct-18-2020

Make an array of your arrays, and an array of your datatables.
The code below uses Pandas as I am more familiar, but the concept is the same.
import pandas as pd

array_of_arrays = [[1,2,3]]
array_of_arrays.append([4,5,6])
array_of_arrays.append([7,8,9])

array_of_datasets = list()
nsets = int(input('Enter number of datasets: '))
for i in range(nsets):
    array_of_datasets.append(pd.DataFrame(array_of_arrays[i%3]))

for i in range(nsets):
    print(array_of_datasets[i])