def createRandomTable(cls, name, nrow, ncol, ncolstr=0, low_int=0, hi_int=100, table_cls=None): """ Creates a table with random integers as values Input: name - name of the table nrow - number of rows ncol - number of columns ncolstr - number of columns with strings low_int - smallest integer hi_int - largest integer table_cls - Table class to use; default is Table """ if table_cls is None: table_cls = cls ncol = int(ncol) nrow = int(nrow) table = cls(name) ncolstr = min(ncol, ncolstr) ncolint = ncol - ncolstr c_list = range(ncol) random.shuffle(c_list) for n in range(ncol): column = Column("Col_" + str(n)) if c_list[n] <= ncolint - 1: values = np.random.randint(low_int, hi_int, nrow) values_ext = values.tolist() else: values_ext = ut.randomWords(nrow) #values_ext.append(None) column.addCells(np.array(values_ext)) table.addColumn(column) table.setFilepath(settings.SCISHEETS_DEFAULT_TABLEFILE) return table
def dataframeToTable(self, table_name, dataframe, names=None): """ Creates a Table from the pandas dataframe. :param str table_name: name of the table :param pd.DataFrame dataframe: :param list-of-str names: names of names in the dataframe that are names in the table. Defaull is all. :return Table table: """ if names is None: names = list(dataframe.columns) table = Table(table_name) for name in names: column = Column(name) column.addCells(dataframe[name], replace=True) table.addColumn(column) return table
def addColumnsToTableFromDataframe(self, dataframe, names=None, column_position=None, table=None): """ Adds columns from a dataframe to a table. If a column of the same name exists, its data is replaced. :param pandas.DataFrame dataframe: :param list-of-str names: names of columns in the dataframe to include. Default (None) is all. :param str column_position: name of the column to place after :return list-of-str names: names of columns added to the table """ if table is None: table = self._table self.updateColumnFromColumnVariables() # Make sure table is current if names is None: names = list(dataframe.columns) index = table.numColumns() # Where to insert new columns if column_position is not None: column = table.columnFromName(column_position) index = table.indexFromColumn(column) + 1 for name in names: if table.isColumnPresent(name): column = table.columnFromName(name) else: if "." in name: import pdb pdb.set_trace() column = Column(name) table.addColumn(column, index=index) index += 1 column.addCells(dataframe[name], replace=True) if column.getParent() is None: import pdb pdb.set_trace() pass root_table = table.getRoot(is_attached=False) root_table.adjustColumnLength() self.setColumnVariables() return names
def addColumnsToTableFromDataframe(self, dataframe, names=None, column_position=None): """ Adds columns from a dataframe to the table. If a column of the same name exists, its data is replaced. :param pandas.DataFrame dataframe: :param list-of-str names: names of columns in the dataframe to include. Default (None) is all. :param str column_position: name of the column to place after :return list-of-str names: names of columns added to the table """ self.updateColumnFromColumnVariables() # Make sure table is current if names is None: names = list(dataframe.columns) if column_position is None: index = self._table.numColumns() else: column = self._table.columnFromName(column_position) index = self._table.indexFromColumn(column) + 1 for name in names: if self._table.isColumnPresent(name): column = self._table.columnFromName(name) else: if "." in name: import pdb; pdb.set_trace() column = Column(name) self._table.addColumn(column, index=index) index += 1 column.addCells(dataframe[name], replace=True) if column.getTable() is None: import pdb; pdb.set_trace() pass self.setColumnVariables() return names