Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

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!

import pandas as pd
df = pd.DataFrame(
    {"col1": [1, 2], "col2": [0.5, 0.75]},
    index=["row1", "row2"],
    columns=["col1", "col2"],
)
df
Loading...
[m for m in dir(df) if m.startswith("to_")]
['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

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

DataFrame.to_records

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