
How to easily share a sample dataframe using df.to_dict()
In many situations, using an approach with df.to_dict() will perfectly suffice! Here are two cases that come to mind:
Case 1: You've got a dataframe built or loaded in Python from a local source
Case 2: You've got a table in another application (like Excel)
The details:
Case 1: You've got a dataframe built or loaded from a local source
Given that you've got a pandas dataframe named df, just
- run df.to_dict() in you console or editor,
- copy the output that is formatted as a dictionary,
- paste the content into pd.DataFrame(
And there are two ways you can handle larger dataframes:
- run df.head(20).to_dict() to only include the first 20 rows, and
- change the format of your dict using, for example, df.to_dict('split') (there are other options besides 'split') to reshape your output to a dict that requires fewer lines.
The above is the detailed content of How Can I Easily Share a Pandas DataFrame Using `df.to_dict()`?. For more information, please follow other related articles on the PHP Chinese website!
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn