0

I am facing a problem in receiving correct date formatted data from the CSV using to_json function of pandas.

import pandas as pd
import json

df = pd.read_csv("C:\\Users\\shubham\\Desktop\\Output\\MasterData.csv")
df1 = df.to_json(orient='records')
print(df1)

Current Output:-

[{"invoiceDate":"18\/08\/2019","amount":1140.87}]

I am expecting Output:- "invoiceDate":"18/08/2019"

I already tried to_json arguments:- date_format = "iso" double_precision = 10, force_ascii = True, date_unit = "ms", default_handler = None), and replace is also not working (df.replace("/","")).

bharatk
  • 4,202
  • 5
  • 16
  • 30
shubham jain
  • 45
  • 11

2 Answers2

0

Create dictionary and write to file with json.dump:

df = pd.DataFrame([{"invoiceDate":"18/08/2019","amount":1140.87}])
print (df)
  invoiceDate   amount
0  18/08/2019  1140.87


import json
with open('data.json', 'w') as f:
    json.dump(df.to_dict(orient='records'), f)

#[{"invoiceDate": "18/08/2019", "amount": 1140.87}]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

I used the replace function in the data-frame to replace forward slash as per my need.

The following code helped me to archive my desire output.

df1 = df.to_json(orient='records',lines=True).replace('\\r\\n', " ")
milanbalazs
  • 4,811
  • 4
  • 23
  • 45
shubham jain
  • 45
  • 11