简体   繁体   中英

How to convert a excel data to json using pandas

I have one excel which is having multiple values like (0,1 and 3)

Here is my excel

在此处输入图片说明

In this excel i've multiple signals as Header and multiple steps as row name

Here,i want to read all rows(steps) value one by one and need to check whether the values are 1 or 3 or 0. If it is 1 or 3 i've to provide the start time and End time and if it is 0 then leave it as it is.

Here i'm explaining how my output should be

  1. i will start reading the data from step1 then user should provide the start time for 1st signal and also for the interval time like([70000000,70100000], here 70000000 is the interval time) it should be same for all steps.

  2. if i will start from step1, if it would 1 or 3 it should print [70000000,70100000] and in next step if it would 1 or 3 it should print [70200000,70300000].

my expected result should be like this

 {'step1':{'state[0]':[70000000,70100000],'state[2]': 
 [70000000,70100000],'state[4]':[70000000,70100000],'state[5]': 
 [70000000,70100000],'state[6]':[70000000,70100000],'state[7]': 
 [70000000,70100000]}}

 {'step2':{'state[0]':0,'state[2]':0,'state[4]': 
 [70200000,70300000],'state[5]':0,'state[6]':0,'state[7]':0}} 

using this i want to create one xml file, exactly i don't know in which approach i can reach to this output. please help me with this

i'm trying like this

import pandas as pd
path = r"C:\Users\uib05928\Desktop\vdy\vdy.xlsx"
df = pd.read_excel(path,sheetname='Sheet3')
list_dict_data = df.T.to_dict().values()
# print "length...", len(list_dict_data)
# print  list_dict_data
num1 = int(input('Enter 1st:'))
num2 = int(input('Enter 2nd:'))
lis = []
for x in list_dict_data:
lis.append({x['steps']: x})

li = []
for i in lis:
for k,v in i.iteritems():
    li.append(v)
l = []
ab = [1.0,3.0]
ze = 0.0
for j in li:
del j['steps']
len_j = len(j)
lis1 = []
for key,val in j.iteritems():
    if j[key] in ab:
        j[key] = [num1,num2]
        l.append(j)
 for a in l:
print a

Thank you in Advance

pandas has a built-in method to convert a DataFrame to json: pandas.DataFrame.to_json

It follows a similar format as df.to_csv(), allowing you to specify the file path and other parameters.

In this example, after the processing and user input is appended to your DataFrame:

df.to_json('filename.xml', orient='split')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM