DataFrame

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

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

Warning

DataFrame objects should not be instantiated manually. For best performance using Python native types or Pandas Dataframes. The API takes care of the conversion for you in the most efficient way it finds.

An object of this class can be used to do the following tasks:

  • Assign values to AMPL entities (once the DataFrame is populated, use set_data() 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 get_values().

A DataFrame object can be created in various ways:

and can be converted to various object types:

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

Create a new DataFrame with specifed index and column headers.

Args:

index: Index column;

columns: Column headers.

__iter__()
get_num_cols()

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

Returns:

The number of columns.

get_num_rows()

Get the number of data rows in this dataframe.

Returns:

The number of rows.

get_num_indices()

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

Returns:

The number of indices needed to access one row of this dataframe.

add_row(*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.

add_column(header, values=None)

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.

get_column(header)

Get the specified column as a view object.

Args:

header: The header of the column.

set_column(header, values)

Set the values of a column.

Args:

header: The header of the column to be set.

values: The values to set.

get_row(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.

get_row_by_index(index)

Get row by numeric index.

Args:

index: Zero-based index of the row to get.

Returns:

The corresponding row.

get_headers()

Get the headers of this DataFrame.

Returns:

The headers of this DataFrame.

set_values(values)

Set the values of a DataFrame from a dictionary.

Args:

values: Dictionary with the values to set.

to_dict()

Return a dictionary with the DataFrame data.

to_list()

Return a list with the DataFrame data.

to_pandas()

Return a pandas DataFrame with the DataFrame data.

classmethod from_dict(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 from_pandas(df, index_names=None)

Create a DataFrame from a pandas DataFrame.

Args:

df: Pandas DataFrame to load. index_names: index names to use.

classmethod from_numpy(data)

Create a DataFrame from a numpy array or matrix.

addColumn(header, values=None)

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.

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.

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 fromNumpy(data)

Create a DataFrame from a numpy array or matrix.

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.

getColumn(header)

Get the specified column as a view object.

Args:

header: The header of the column.

getHeaders()

Get the headers of this DataFrame.

Returns:

The headers of this DataFrame.

getNumCols()

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

Returns:

The number of columns.

getNumIndices()

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

Returns:

The number of indices needed to access one row of this dataframe.

getNumRows()

Get the number of data rows in this dataframe.

Returns:

The number of rows.

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.

setColumn(header, values)

Set the values of a column.

Args:

header: The header of the column to be set.

values: The values to set.

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.

__module__ = 'amplpy.dataframe'