DataFrame

class amplpy.DataFrame(index, columns=(), **kwargs)

A DataFrame object, used to communicate data to and from the AMPL entities.

An object of this class can be used to do the following tasks: - Assign values to AMPL entities (once the DataFrame is populated, use setData() to assign its values to the modelling entities in its columns) - Get values from AMPL, decoupling the values from the AMPL entities they originate via getValues().

A DataFrame object can be created in various ways.

  • Create a skeleton by specifiying manually the indexing columns and the column headers.
  • Get values from AMPL, decoupling the values from the AMPL entities they originate from (via getValues()).

Populating a DataFrame object can be done adding row by row to a pre-existing skeleton via addRow(), setting whole columns of a pre-existing skeleton via setColumn() or adding columns (including indexing columns) via addColumn().

Modifying a DataFrame object can be done via setColumn() or, item by item, via setValue().

Accessing data in a DataFrame can be done row by row using getRow() or by column via getColumn().

__init__(index, columns=(), **kwargs)

Create a new DataFrame with specifed index and column headers.

Args:

index: Index column;

columns: Column headers.

__iter__()
getNumCols()

Get the total number of columns in this dataframe (indexarity + number of values).

Returns:
The number of columns.
getNumRows()

Get the number of data rows in this dataframe.

Returns:
The number of rows.
getNumIndices()

Get the number of indices (the indexarity) of this dataframe.

Returns:
The number of indices needed to access one row of this dataframe.
addRow(*value)

Add a row to the DataFrame. The size of the tuple must be equal to the total number of columns in the dataframe.

Args:
value: A single argument with a tuple containing all the values for the row to be added, or multiple arguments with the values for each column.
addColumn(header, values=[])

Add a new column with the corresponding header and values to the dataframe.

Args:

header: The name of the new column.

values: A list of size getNumRows() with all the values of the new column.

getColumn(header)

Get the specified column as a view object.

Args:
header: The header of the column.
setColumn(header, values)

Set the values of a column.

Args:

header: The header of the column to be set.

values: The values to set.

getRow(key)

Get a row by value of the indexing columns. If the index is not specified, gets the only row of a dataframe with no indexing columns.

Args:
key: Tuple representing the index of the desired row.
Returns:
The row.
getRowByIndex(index)

Get row by numeric index.

Args:
index: Zero-based index of the row to get.
Returns:
The corresponding row.
getHeaders()

Get the headers of this DataFrame.

Returns:
The headers of this DataFrame.
setValues(values)

Set the values of a DataFrame from a dictionary.

Args:
values: Dictionary with the values to set.
toDict()

Return a dictionary with the DataFrame data.

toList()

Return a list with the DataFrame data.

toPandas()

Return a pandas DataFrame with the DataFrame data.

classmethod fromDict(dic, index_names=None, column_names=None)

Create a DataFrame from a dictionary.

Args:
dic: dictionary to load. index_names: index names to use. column_names: column names to use.
classmethod fromPandas(df, index_names=None)

Create a DataFrame from a pandas DataFrame.

Args:
df: Pandas DataFrame to load. index_names: index names to use.
classmethod fromNumpy(data)

Create a DataFrame from a numpy array or matrix.

__module__ = 'amplpy.dataframe'