Pandas interoperability (pandas_compat)

Orange.data.pandas_compat module provides functions to convert between pandas.DataFrame and Orange.data.Table. These functions enable integration of Orange's data structures with the pandas library, enabling users to shift between the frameworks.

Orange.data.pandas_compat.table_from_frame(df, *, force_nominal=False, variables=None)[source]

Convert pandas DataFrame to Orange.data.Table.

Parameters:
  • df (pandas DataFrame)

  • force_nominal (bool, (default=False)) -- Force all string variables to be nominal.

  • variables (list of Variable, optional)

Return type:

Orange.data.Table

Orange.data.pandas_compat.table_to_frame(tab, include_metas=False)[source]

Convert Orange.data.Table to pandas.DataFrame

Parameters:
  • tab (Table)

  • include_metas (bool, (default=False)) -- Include table metas into dataframe.

Return type:

pandas.DataFrame

Example

>>> import pandas as pd
>>> from Orange.data import Table
>>> from Orange.data.pandas_compat import table_from_frame, table_to_frame
>>> df = pd.DataFrame({'A': [1, 2, 3], 'B': [4.0, 5.0, 6.0], 'C': ['a', 'b', 'c']})
>>> df
   A    B  C
0  1  4.0  a
1  2  5.0  b
2  3  6.0  c
>>> table = table_from_frame(df)
>>> table
[[1, 4] {a},
 [2, 5] {b},
 [3, 6] {c}
]

Note that the non-numeric column 'C' becomes a meta attribute in the resulting table. To set it to a categorical variable, use force_nominal=True:

>>> table = table_from_frame(df, force_nominal=True)
[[1, 4, a],
 [2, 5, b],
 [3, 6, c]
]

To convert back to a pandas DataFrame, use table_to_frame():

>>> frame = table_to_frame(table)
>>> frame
   A    B
0  1  4.0
1  2  5.0
2  3  6.0