Skip to content
Snippets Groups Projects
Commit f1ebc03c authored by amilashanaka's avatar amilashanaka
Browse files

convert to dataframe

parent 45c7906f
No related branches found
No related tags found
No related merge requests found
...@@ -46,10 +46,28 @@ import pmdarima as pm ...@@ -46,10 +46,28 @@ import pmdarima as pm
def start(): def start():
# read incomming json data
data=request.get_json() data=request.get_json()
# convert json data into pandas data structure
data_arr=json.dumps(data)
df=pd.read_json(data_arr)
df=df.transpose()
df.rename(columns = {'Product_Code': 'ProductCode',
'Product_Category': 'ProductCategory',
'Order_Demand': 'OrderDemand'}, inplace = True)
#check is null
df.isnull().sum()
return data # remove nan values
df.dropna(inplace=True)
print(df.isnull().sum())
return data_arr
def read_json(income_data): def read_json(income_data):
......
import csv
import json
import pandas as pd
from pymongo import MongoClient
# # get client instence
# client = MongoClient()
# # Connect to database
# db=client.station
# #create station collection
# station = db.station
# # Read csv file to pandas data frame
# df=pd.read_csv("product_demnd.csv",engine='python')
# # df = df.rename({'Unnamed: 0.1':'ID','Unnamed: 0': 'Read ID'}, axis=1)
# #Set Data Frame index to ID
# df=df.set_index('ID')
# #save to csv file
# df.to_csv('out.csv')
#function convert to csv file to json file
def make_json(csvFilePath, jsonFilePath):
# create a data dictionary
data = {}
# Open a csv reader called DictReader
with open(csvFilePath, encoding='utf-8') as csvf:
csvReader = csv.DictReader(csvf)
# Convert each row into a dictionary
# and add it to data
for rows in csvReader:
# Assuming a column named 'ID' to
# be the primary key
key = rows['Product_Code']
data[key] = rows
# Open a json writer, and use the json.dumps()
# function to dump data
with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
jsonf.write(json.dumps(data, indent=4))
#call function convert csv file to json file format
make_json('product__demnd.csv','input_data.json')
# file_data= []
# #open json file and read each line to json object
# with open('nosql.json') as f:
# file_data.append(json.load(f))
# #inset each object to station collection
# station.insert_many(file_data)
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment