Ben Chuanlong Du's Blog

It is never too late to learn.

Convert Pandas DataFrame to Other Format

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

In [1]:
import pandas as pd
In [2]:
df = pd.DataFrame(
    {"col1": [1, 2], "col2": [0.5, 0.75]},
    index=["row1", "row2"],
    columns=["col1", "col2"],
)
df
Out[2]:
col1 col2
row1 1 0.50
row2 2 0.75
In [16]:
[m for m in dir(df) if m.startswith("to_")]
Out[16]:
['to_clipboard',
 'to_csv',
 'to_dict',
 'to_excel',
 'to_feather',
 'to_gbq',
 'to_hdf',
 'to_html',
 'to_json',
 'to_latex',
 'to_markdown',
 'to_numpy',
 'to_parquet',
 'to_period',
 'to_pickle',
 'to_records',
 'to_sql',
 'to_stata',
 'to_string',
 'to_timestamp',
 'to_xarray',
 'to_xml']

DataFrame.to_dict

In [3]:
df.to_dict()
Out[3]:
{'col1': {'row1': 1, 'row2': 2}, 'col2': {'row1': 0.5, 'row2': 0.75}}
In [5]:
df.to_dict("list")
Out[5]:
{'col1': [1, 2], 'col2': [0.5, 0.75]}
In [6]:
df.to_dict("series")
Out[6]:
{'col1': row1    1
 row2    2
 Name: col1, dtype: int64,
 'col2': row1    0.50
 row2    0.75
 Name: col2, dtype: float64}
In [7]:
df.to_dict("split")
Out[7]:
{'index': ['row1', 'row2'],
 'columns': ['col1', 'col2'],
 'data': [[1, 0.5], [2, 0.75]]}
In [8]:
df.to_dict("records")
Out[8]:
[{'col1': 1, 'col2': 0.5}, {'col1': 2, 'col2': 0.75}]
In [13]:
df.to_dict("index")
Out[13]:
{'row1': {'col1': 1, 'col2': 0.5}, 'row2': {'col1': 2, 'col2': 0.75}}

DataFrame.to_records

In [12]:
df.to_records()
Out[12]:
rec.array([('row1', 1, 0.5 ), ('row2', 2, 0.75)],
          dtype=[('index', 'O'), ('col1', '<i8'), ('col2', '<f8')])
In [20]:
df.to_records(index=False)
Out[20]:
rec.array([(1, 0.5 ), (2, 0.75)],
          dtype=[('col1', '<i8'), ('col2', '<f8')])
In [ ]:
 

Comments