IOLink Python 1.11.0
Loading...
Searching...
No Matches
createDataFrame.py

This code shows how to create a DataFrame object and add data to it.

This code shows how to create a DataFrame object and add data to it. create a DataFrame object with 4 columns and 3 rows and fill it with the following data:

Index Diameter(μm) Elongation factor Type
1 6.52 0.93 Nucleus
2 2.47 0.37 Mitochondrion
3 5.78 0.86 Nucleus

Then another Formula column is added to give the result of the multiplication of Diameter and Elongation factor columns.

1import iolink
2from array import array
3
4
17
18# Create a DataFrame object with 4 columns, using given values:
19# - Indexer (int)
20# - Diameter (double)
21# - Elongation Factor (double)
22# - Type (string)
23def create_dataframeview(diameterValues, elongFactValues, typeValues) -> iolink.DataFrameView:
24 # each list should have the same size
25 if len(diameterValues) != len(elongFactValues) or len(diameterValues) != len(typeValues):
26 raise ValueError("All lists should have the same size")
27
28 numRows = len(diameterValues)
29
30 # create a DataFrame object with 4 columns and the number of rows equal to the size of the given data
31 columnNames = ["Index", "Diameter", "Elongation Factor", "Type"]
32 columnTypes = [iolink.DataTypeId.UINT64, iolink.DataTypeId.DOUBLE, iolink.DataTypeId.DOUBLE, iolink.DataTypeId.UTF8_STRING]
33 dataFrame = iolink.DataFrameViewFactory.allocate([4, numRows], columnNames, columnTypes)
34
35 # set the unit of the Diameter column
36 dataFrame.set_unit(dataFrame.column_index("Diameter"), "mm")
37
38 # create the list of values of the index
39 indexValues = list(range(1, numRows + 1))
40
41 # fill each column with the given data, one shot
42 dataFrame.write(dataFrame.column_index("Index"), 0, numRows, indexValues)
43 dataFrame.write(dataFrame.column_index("Diameter"), 0, numRows, diameterValues)
44 dataFrame.write(dataFrame.column_index("Elongation Factor"), 0, numRows, elongFactValues)
45 dataFrame.write(dataFrame.column_index("Type"), 0, numRows, typeValues)
46
47 return dataFrame
48
49
50# Add a new column "formula" to the DataFrame object which is the result of the multiplication of the Diameter and
51# Elongation Factor columns
52def add_formula_to_dataFrame(df: iolink.DataFrameView):
53 # add a new column "formula" to the DataFrame object
54 df.add_column("Formula", iolink.DataTypeId.DOUBLE)
55
56 # get the number of rows in the DataFrame object
57 numRows = df.shape[1]
58
59 # create a vector to store the result of the multiplication of the Diameter and Elongation Factor columns
60 formulaValues = array('d',[0] * numRows)
61
62 # calculate the result of the multiplication of the Diameter and Elongation Factor columns
63 for i in range(0, numRows):
64 formulaValues[i] = df.at("Diameter", i) * df.at("Elongation Factor", i)
65 # write the result of the multiplication of the Diameter and Elongation Factor columns to the new column "formula"
66 df.set_at("Formula", i, formulaValues[i])
67
68# create dataframe
69df = create_dataframeview([6.52, 2.47, 5.78], [0.93, 0.37, 0.86], ["Nucleus", "Mitochondrion", "Nucleus"])
70
71# print the DataFrame object
72print(df)
73
74# add one column "formula" to the DataFrame object which contains the result of the multiplication of the Diameter
75# and Elongation Factor columns
76add_formula_to_dataFrame(df)
77
78# print the DataFrame object after adding the new column "formula"
79print(df)