databricks.koalas.DataFrame.to_koalas

DataFrame.to_koalas(index_col: Union[str, List[str], None] = None)[source]

Converts the existing DataFrame into a Koalas DataFrame.

This method is monkey-patched into Spark’s DataFrame and can be used to convert a Spark DataFrame into a Koalas DataFrame. If running on an existing Koalas DataFrame, the method returns itself.

If a Koalas DataFrame is converted to a Spark DataFrame and then back to Koalas, it will lose the index information and the original index will be turned into a normal column.

Parameters
index_col: str or list of str, optional, default: None

Index column of table in Spark.

Examples

>>> df = ks.DataFrame({'col1': [1, 2], 'col2': [3, 4]}, columns=['col1', 'col2'])
>>> df
   col1  col2
0     1     3
1     2     4
>>> spark_df = df.to_spark()
>>> spark_df
DataFrame[col1: bigint, col2: bigint]
>>> kdf = spark_df.to_koalas()
>>> kdf
   col1  col2
0     1     3
1     2     4

We can specify the index columns.

>>> kdf = spark_df.to_koalas(index_col='col1')
>>> kdf  
      col2
col1
1        3
2        4

Calling to_koalas on a Koalas DataFrame simply returns itself.

>>> df.to_koalas()
   col1  col2
0     1     3
1     2     4
Scroll To Top