Commit 8e366299 by mohammed.shibili

pivot error correction and header change

parent 35887f8f
...@@ -6,25 +6,33 @@ from script.core.handlers.pivot import to_pivot ...@@ -6,25 +6,33 @@ from script.core.handlers.pivot import to_pivot
# creating json # creating json
def to_json(new_dataframe, label_dataframe, melting_data): def to_json(new_dataframe, label_dataframe, melting_data):
header_dict = {'header': dict(zip(label_dataframe.columns, new_dataframe.columns))} head_list = []
for index, header in enumerate(new_dataframe.columns):
head_list.append({header: label_dataframe[index]})
header_dict = {'header': head_list}
data_dict = {'body': new_dataframe.to_dict(orient='records')} data_dict = {'body': new_dataframe.to_dict(orient='records')}
json_dict = {**header_dict, **data_dict} json_dict = {**header_dict, **data_dict}
# for pivot data
pivoted_data = to_pivot(new_dataframe)
pivoted_dataframe = {'body': pivoted_data.to_dict(orient='records')}
pivot_dict = {**header_dict, **pivoted_dataframe}
try: try:
with open(json_path, 'w') as f: with open(json_path, 'w') as f:
json.dump(json_dict, f, indent=4) json.dump(json_dict, f, indent=4)
print(f'parsing of {json_dict} done\n')
except Exception as e: except Exception as e:
print("error while parsing ", e) print("error while parsing ", e)
# for pivot data
pivoted_data = to_pivot(new_dataframe)
pivoted_dataframe = {'body': pivoted_data.to_dict(orient='records')}
pivot_dict = {**header_dict, **pivoted_dataframe}
# pivoted data to json # pivoted data to json
try: try:
with open(pivot_path, 'w') as w: with open(pivot_path, 'w') as w:
json.dump(pivot_dict, w, indent=4) json.dump(pivot_dict, w, indent=4)
print(f'parsing of pivoted data {pivot_dict} done\n')
except Exception as e: except Exception as e:
print("error while parsing of pivot", e) print("error while parsing of pivot", e)
# for melt # for melt
melted_dataframe = {'body': melting_data.to_dict(orient='records')} melted_dataframe = {'body': melting_data.to_dict(orient='records')}
...@@ -32,6 +40,7 @@ def to_json(new_dataframe, label_dataframe, melting_data): ...@@ -32,6 +40,7 @@ def to_json(new_dataframe, label_dataframe, melting_data):
try: try:
with open(melted_path, 'w') as w: with open(melted_path, 'w') as w:
json.dump(melted_dict, w, indent=4) json.dump(melted_dict, w, indent=4)
print(f'melting of {melted_dict} done\n')
except Exception as e: except Exception as e:
print("error while parsing melted data", e) print("error while parsing melted data", e)
...@@ -42,5 +51,6 @@ def to_json(new_dataframe, label_dataframe, melting_data): ...@@ -42,5 +51,6 @@ def to_json(new_dataframe, label_dataframe, melting_data):
try: try:
with open(merged_path, 'w') as x: with open(merged_path, 'w') as x:
json.dump(merged_dict, x, indent=4) json.dump(merged_dict, x, indent=4)
print(f'merging of {merged_dict} done')
except Exception as e: except Exception as e:
print("error while parsing merged data", e) print("error while parsing merged data", e)
...@@ -15,9 +15,9 @@ def json_parsing(): ...@@ -15,9 +15,9 @@ def json_parsing():
new_dataframe = pd.concat([new_dataframe, dataset.tail(10)], ignore_index=True) new_dataframe = pd.concat([new_dataframe, dataset.tail(10)], ignore_index=True)
# dataframe for setting labels # dataframe for setting labels
label_dataframe = pd.DataFrame( label_dataframe = ('timestamp', 'kwh', 'kvah', 'kw', 'kva', 'current_value')
{'label1': '', 'label2': '', 'label3': '', 'label4': '', 'label5': '', 'label6': ''},
index=['column1', 'column2', 'column3', 'column4', 'column5', 'column6', ]) # for changing time format
new_dataframe = time_format(new_dataframe) new_dataframe = time_format(new_dataframe)
# call for parsing to json # call for parsing to json
......
{ {
"header": { "header": [
"label1": "Timestamp", {
"label2": "kWh", "Timestamp": "timestamp"
"label3": "kVAh", },
"label4": "kW", {
"label5": "kVA", "kWh": "kwh"
"label6": "current"
}, },
{
"kVAh": "kvah"
},
{
"kW": "kw"
},
{
"kVA": "kva"
},
{
"current": "current_value"
}
],
"body": [ "body": [
{ {
"Timestamp": "21:00:00 01-12-19", "Timestamp": "21:00:00 01-12-19",
......
{ {
"header": { "header": [
"label1": "Timestamp", {
"label2": "kWh", "Timestamp": "timestamp"
"label3": "kVAh", },
"label4": "kW", {
"label5": "kVA", "kWh": "kwh"
"label6": "current"
}, },
{
"kVAh": "kvah"
},
{
"kW": "kw"
},
{
"kVA": "kva"
},
{
"current": "current_value"
}
],
"body": [ "body": [
{ {
"Timestamp": "21:00:00 01-12-19", "Timestamp": "21:00:00 01-12-19",
......
{ {
"header": { "header": [
"label1": "Timestamp", {
"label2": "kWh", "Timestamp": "timestamp"
"label3": "kVAh", },
"label4": "kW", {
"label5": "kVA", "kWh": "kwh"
"label6": "current"
}, },
{
"kVAh": "kvah"
},
{
"kW": "kw"
},
{
"kVA": "kva"
},
{
"current": "current_value"
}
],
"body": [ "body": [
{ {
"Timestamp": "21:00:00 01-12-19", "Timestamp": "21:00:00 01-12-19",
......
{ {
"header": { "header": [
"label1": "Timestamp", {
"label2": "kWh", "Timestamp": "timestamp"
"label3": "kVAh", },
"label4": "kW", {
"label5": "kVA", "kWh": "kwh"
"label6": "current"
}, },
{
"kVAh": "kvah"
},
{
"kW": "kw"
},
{
"kVA": "kva"
},
{
"current": "current_value"
}
],
"body": [ "body": [
{ {
"current": 283.2255350748698, "current": 283.2255350748698,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment