Skip to content
Sandny Blog
  • Home
  • Java
  • JavaScript
  • AWS
  • Embedded
  • About
Pandas Dataframe to a file and read back Python

How to save pandas Dataframe to a file and…

  • September 16, 2022September 16, 2022
  • by Coder Kai

Pandas DataFrame is a very useful data type in Python to format and order data in different ways. One of its use is to write Dataframe to a file and read back to a DataFrame when it is needed.

1. DataFrame to CSV

In this example, we will write DataFrame into a CSV file

import pandas as pd

d = {'Name': ['John Doe', 'Foo', 'Tony Stark'], 'Age': [10, 12, 48]}
df = pd.DataFrame(data=d)

df.to_csv('test.csv', index=False)

This will create a file called test.csv in the directory, you have run the python code.

Python Dataframe to CSV

Note that here we have provided index=False so it will not provide the index as a column. If you want the index you can just run to_csv function without params

df.to_csv('test.csv')
Python Dataframe to CSV with the index of the rows

You can read this CSV by using the read_csv function.

import pandas as pd

dfr = pd.read_csv('test.csv')
print(dfr)

This will print the data in the console as a formated table

When you have data with commas, you can change the seperator to a different one using sep= parameter. Here I have used | pipe symbol and following is the result

import pandas as pd

d = {'Name': ['John Doe', 'Foo', 'Tony Stark'], 'Age': [10, 12, 48]}
df = pd.DataFrame(data=d)

df.to_csv('test.csv', index=False, sep="|")

dfr = pd.read_csv('test.csv')
print(dfr)
Dataframe to CSV with a different seperator

2. Dataframe to JSON

When saving JSON, we have to provide the rows, columns, and indexes as well if you need to guarantee order of data.

For that, we can provide orient parameter when converting to JSON.

import pandas as pd

d = {'Name': ['John Doe', 'Foo', 'Tony Stark'], 'Age': [10, 12, 48]}
df = pd.DataFrame(data=d)

df.to_json('test.json', orient='split')

dfr = pd.read_json('test.json', orient="split")
print(dfr)

This saves the test.json file in the following format

DataFrame to json file

You can see the three main attributes columns, index, data defines how the data should be in the DataFrame.

There are other orients that you can save the DataFrame to JSON files

orient=records

Array of records as each row. No loss as the column title is available. Takes space as column title is always duplicated

orient=columns

Each column data is represented by the index

orient=values

Array of each rows of data as arrays. Similar to records but data row represented as an array. Saves space but column header is missing.

orient=table

Much more details as a table structure. Data type and pandas version all reserved

Following are some useful References to write Dataframe to a file and read back

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_json.html

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_csv.html

SSL Websocket proxy with Nginx for Graphql Subscriptions
How to copy files from S3 to PostgreSQL RDS
Coder Kai
A humble developer

Related articles

Copy files from S3 to PostgreSQL RDS
How to copy files from…

Categories

  • android 3
  • Apollo Client 1
  • AWS 8
    • AppSync 5
    • EC2 1
    • EKS 1
    • Route53 1
    • S3 1
  • AWS Amplify 1
  • Chakra UI 1
  • Docker 1
  • Embedded 1
  • EmberJS 1
  • FCM 1
  • Godaddy 1
  • GraphQL 3
  • ios 1
  • Jasper 1
  • Java 10
    • Java 11 1
    • Java 14 1
  • JavaEE 2
  • JavaScript 39
    • Express.js 4
    • Javascript Guide 7
    • Node.js 3
    • react-native 4
    • React.js 17
    • Typescript 1
  • Kubernetes 1
  • machine learning 1
  • Maven 2
  • OCaml 3
  • PostgreSQL 1
  • Python 2
  • react-native 4
  • ReactJS 3
  • sass 1
  • Server 6
  • spark 1
  • Terraform 2
  • Ubuntu 4
  • Uncategorized 1
  • webpack 2

Recent Comments

  • Aaliyah3552 on How to save pandas Dataframe to a file and read back
  • binance account on Minimum supported Gradle version is 7.0. Current version is 6.5
  • binance us registracija on How to add styles to stripe elements without using CardElement

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Archives

  • October 2022 3
  • September 2022 7
  • May 2022 1
  • December 2021 1
  • August 2021 1
  • July 2021 6
  • June 2021 3
  • February 2021 1
  • July 2020 1
  • December 2019 5
  • November 2019 6
  • October 2019 3
  • August 2019 1
  • March 2019 1
  • February 2019 1
  • January 2019 2
  • December 2018 1
  • September 2018 2
  • August 2018 1
  • June 2018 1
  • February 2018 1
  • November 2017 2
  • October 2017 5
  • September 2017 1
  • June 2017 1
  • May 2017 10
Sandny Blog space
Theme by Colorlib Powered by WordPress